From various Internet posts and recent experience, I have observed that you cannot rely on proc_close returning the accurate return code of the child process. The return code also depends on wether or not you read from the stdout/stderr pipes, as my example shows. I work around this by writing the exit code to an additional file descriptor.
<?
$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('pipe', 'w'), // stderr is a pipe that the child will write to
);
$proc = @proc_open("/bin/ls -l /etc/passwd", $descriptorspec, $pipes);
fclose($pipes[0]);
$output = array();
while (!feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n"));
fclose($pipes[1]);
while (!feof($pipes[2])) array_push($output, rtrim(fgets($pipes[2],1024),"\n"));
fclose($pipes[2]);
$exit=proc_close($proc);
print_r($output);
echo "exitcode $exit\n\n";
$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('pipe', 'w'), // stderr is a pipe that the child will write to
);
$proc = @proc_open("/bin/ls -l /etc/passwd", $descriptorspec, $pipes);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$exit=proc_close($proc);
echo "exitcode $exit\n\n";
$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('pipe', 'w'), // stderr is a pipe that the child will write to
3 => array('pipe', 'w'), // stderr is a pipe that the child will write to
);
$proc = @proc_open("/bin/ls -l /etc/passwd;echo $? >&3", $descriptorspec, $pipes);
fclose($pipes[0]);
$output = array();
//comment next line to get correct exicode
while (!feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n"));
fclose($pipes[1]);
while (!feof($pipes[2])) array_push($output, rtrim(fgets($pipes[2],1024),"\n"));
fclose($pipes[2]);
if (!feof($pipes[3])) $output['exitcode']=rtrim(fgets($pipes[3],5),"\n");
fclose($pipes[3]);
proc_close($proc);
print_r($output);
?>
Outputs on my system:
Array
(
[0] => -rw-r--r-- 1 root root 1460 2005-09-02 09:52 /etc/passwd
[1] =>
[2] =>
)
exitcode -1
exitcode 1
Array
(
[0] => -rw-r--r-- 1 root root 1460 2005-09-02 09:52 /etc/passwd
[1] =>
[2] =>
[exitcode] => 0
)