Re: Fwd: [PHP] proc_open and buffer limit?
| From: | Wez Furlong | Date: | Mon, 23 Jan 2006 14:08:55 +0000 |
| Subject: | Re: Fwd: [PHP] proc_open and buffer limit? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-21647@lists.php.net to get a copy of this message | ||
There are quite a few bad streams usages there.
I'd rewrite your code like this:
$words = fopen('/usr/share/dict/words', 'r');
stream_copy_to_stream($words, $pipes[0]);
fclose($pipes[0]);
fclose($words);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
My guess at the cause of the problem was that you're tying to write
2MB into a pipe, and the underlying write() syscall could only write
64k; since you're ignoring the return code from fwrite(), you're
missing this vital fact.
Using the streams functions in PHP 5 helps you to write code that
"does what I mean", and makes for shorter, more readable code.
--Wez.
PS: http://netevil.org/talks/PHP-Streams-Lucky-Dip.pdf
has some tips and insider knowledge on using streams in PHP.
On 1/23/06, Mark Krenz <mark@suso.org> wrote:
> stream_set_blocking($pipes[0], FALSE);
> stream_set_blocking($pipes[1], FALSE);
>
> $handle = fopen("/usr/share/dict/words", "r");
> while (!feof($handle)) {
> $input .= fread($handle, 8192);
> }
>
> fwrite($pipes[0], $input);
> fclose($handle);
> fclose($pipes[0]);
>
> while (!feof($pipes[1])) {
> $output .= fgets($pipes[1], 8192);
> }
> fclose($pipes[1]);
>
> print "<PRE>$output</PRE><BR><BR>\n";
>
> $return_value = proc_close($process);
>
> echo "command returned $return_value\n";
> }