note 61952 added to function.proc-open
| From: | richardat2006dotattererdotnet at osu1 dot php dot net | Date: | Wed, 15 Feb 2006 21:42:21 +0000 |
| Subject: | note 61952 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103801@lists.php.net to get a copy of this message | ||
The following code works for piping large amounts of data through a filtering program. I find it
very weird that such a lot of code is needed for this task. On entry, $stdin contains the standard
input for the program. Tested on Debian Linux with PHP 5.0.5.
$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;
$done = 0;
stream_set_blocking($pipes[0], 0); // Make stdin/stdout/stderr non-blocking
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
while (TRUE) {
$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 (!empty($tx)) {
fwrite($pipes[0], substr($stdin, $txOff, 4096));
$txOff += 4096;
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; }
}
}
if ($txOff >= $txLen && $stdoutDone && $stderrDone) break;
}
$returnValue = proc_close($process);
----
Server IP: 217.160.72.57
Probable Submitter: 85.181.13.141
----
Manual Page -- http://www.php.net/manual/en/function.proc-open.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61952
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61952&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61952&report=yes
Search -- http://master.php.net/manage/user-notes.php