【发布时间】:2014-01-22 12:18:04
【问题描述】:
我是 php 新手。当我阅读使用 proc_open() 从其官方文档中执行来自 php 的命令时,我发现了这个示例:
<?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";
}
?>
为什么我们没有使用 fclose() 关闭第三个(管道 [2])文件描述符,否则它会自动关闭?
我试图关闭它,但它给出了警告:
PHP Notice: Undefined offset: 1 in test.php on line 121
PHP Warning: fclose() expects parameter 1 to be resource, null given in test.php on line 121
【问题讨论】:
-
如果您使用文件路径和
file类型描述符,proc_open()将处理此文件上的所有操作,因此您不会取回可以写入或读取的资源。
标签: php