`proc_open` is a function in PHP that allows you to execute a command and open file pointers for input/output. It gives the calling script more control over the execution of external processes.
Here is the syntax of this function:
“`php
proc_open ( string $cmd , array &$descriptorspec , array &$pipes [, string $cwd [, array $env [, array $other_options ]]] ) : resource|false
“`
**Parameters:**
1. `$cmd`: A command as a string, which will be executed directly.
2. `&$descriptorspec`: An indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process.
3. `&$pipes`: Will be set to an indexed array of file pointers that correspond to PHP’s end of any pipes that are created.
4. `$cwd` (Optional): The initial working dir for the command. This must be an absolute directory path, or null if you want PHP to use the default value.
5. `$env` (Optional): An array with environment variables to set for the command. The array values are used as strings.
6. `$other_options` (Optional): An array of other options.
**Return Value:** This function returns a resource representing the process, which should be freed using `proc_close()` when you are finished with it. If not successful, it returns FALSE.
For example, here is a basic use of `proc_open`:
“`php
$descriptorspec = array(
0 => array(“pipe”, “r”), // stdin
1 => array(“pipe”, “w”), // stdout
2 => array(“pipe”, “w”) // stderr
);
$process = proc_open(‘ls’, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], ”);
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);
echo “command returned $return_value\n”;
}
“`
In this example, ‘ls’ command is run, and its output is read through `$pipes`.
暂无评论内容