note 62783 added to function.proc-open

From: Date: Thu, 09 Mar 2006 15:11:35 +0000
Subject: note 62783 added to function.proc-open
Groups: php.notes 
Request: Send a blank email to php-notes+get-105146@lists.php.net to get a copy of this message
In reference to richard's comment below: http://il2.php.net/manual/en/function.proc-open.php#62484 there is the possible problem that the process may die before you have finished writing to it. This seems to fix the problem: $descriptorSpec = array(0 => array("pipe", "r"), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $process = proc_open($command, $descriptorSpec, $pipes); $txOff = 0; $txLen = strlen($stdin); $stdout = ''; $stdoutDone = FALSE; $stderr = ''; $stderrDone = FALSE; stream_set_blocking($pipes[0], 0); // Make stdin/stdout/stderr non-blocking stream_set_blocking($pipes[1], 0); stream_set_blocking($pipes[2], 0); $all_is_good = true; while ($all_is_good) { $rx = array(); // The program's stdout/stderr if (!$stdoutDone) $rx[] = $pipes[1]; if (!$stderrDone) $rx[] = $pipes[2]; $tx = array(); // The program's stdin if ($txOff < $txLen) $tx[] = $pipes[0]; stream_select($rx, $tx, $ex = NULL, NULL, NULL); // Block til r/w possible if ($txOff < $txLen) { $txRet = fwrite($pipes[0], substr($stdin, $txOff, 8192)); if ($txRet > 0 ) $txOff += $txRet; if ($txOff >= $txLen) fclose($pipes[0]); } foreach ($rx as $r) { if ($r == $pipes[1]) { $stdout .= fread($pipes[1], 8192); if (feof($pipes[1])) { fclose($pipes[1]); $stdoutDone = TRUE; } } else if ($r == $pipes[2]) { $stderr .= fread($pipes[2], 8192); if (feof($pipes[2])) { fclose($pipes[2]); $stderrDone = TRUE; } } } $all_is_good = is_resource($process) && ($txOff < $txLen) && (!$stdoutDone) && (!$stderrDone); } $returnValue = proc_close($process); ---- Server IP: 194.153.94.11 Probable Submitter: 132.72.40.247 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.proc-open.php Edit -- http://master.php.net/note/edit/62783 Del: integrated -- http://master.php.net/note/delete/62783/integrated Del: useless -- http://master.php.net/note/delete/62783/useless Del: bad code -- http://master.php.net/note/delete/62783/bad+code Del: spam -- http://master.php.net/note/delete/62783/spam Del: non-english -- http://master.php.net/note/delete/62783/non-english Del: in docs -- http://master.php.net/note/delete/62783/in+docs Del: other reasons-- http://master.php.net/note/delete/62783 Reject -- http://master.php.net/note/reject/62783 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#105146) next »