proc_open

PHP -> Funkcie -> Spúšťacie funkcie PHP -> proc_open

Syntax

resource proc_open ( string $cmd, array $descriptorspec, array &$pipes [, string $cwd [, array $env [, array $other_options] ] ] )

Popis

Príkaz jazyka PHP
proc_open() is similar to popen but provides a much greater degree of control over the program execution.

Parametre

cmd

descriptorspec

pipes

cwd

env

other_options


Return Values

Returns a resource representing the process, which should be freed using proc_close when you are finished with it. On failure returns FALSE.

ChangeLog

Version
Description
6.0.0
Added the context and binary_pipes options to the other_options parameter.
5.2.1
Added the bypass_shell option to the other_options parameter.
5.0.0
Added the cwd, env and other_options parameters.



Poznámky

Note: Windows compatibility: Descriptors beyond 2 (stderr) are made available to the child process as inheritable handles, but since the Windows architecture does not associate file descriptor numbers with low-level handles, the child process does not (yet) have a means of accessing those handles. Stdin, stdout and stderr work as expected.

Note: If you only need a uni-directional (one-way) process pipe, use popen instead, as it is much easier to use.

Príklad

<?php
$descriptorspec = array(
   0 => array("pipe", "r")// stdin is a pipe that the child will read from
   1 => array("pipe", "w")// stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>


The above example will output something similar to:
Array
(
    [some_option] => aeiou
    [PWD] => /tmp
    [SHLVL] => 1
    [_] => /usr/local/bin/php
)
command returned 0


Pozri aj

popen, exec, system, passthru, stream_select