我是新來的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";
}
?>
爲什麼我們還沒有關閉使用第三(管[2])文件描述符fclose()還是會自動關閉?
我試圖關閉它,但隨後它會發出警告:
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()'將處理該文件的所有操作,這樣你就不會回來,你可以寫或讀的資源。 – piotrekkr