Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:21650 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82305 invoked by uid 1010); 23 Jan 2006 16:32:35 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 82290 invoked from network); 23 Jan 2006 16:32:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jan 2006 16:32:35 -0000 X-Host-Fingerprint: 216.9.132.134 mail.suso.org Linux 2.5 (sometimes 2.4) (4) Received: from ([216.9.132.134:49804] helo=arvo.suso.org) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id B6/45-06819-22505D34 for ; Mon, 23 Jan 2006 11:32:34 -0500 Received: by arvo.suso.org (Postfix, from userid 509) id 560D41301B6; Mon, 23 Jan 2006 16:35:16 +0000 (GMT) Date: Mon, 23 Jan 2006 16:35:16 +0000 To: internals@lists.php.net Message-ID: <20060123163515.GB13342@arvo.suso.org> References: <20060123031050.GD23755@arvo.suso.org> <4e89b4260601230608p18596cdchddcf0a460b79d37f@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4e89b4260601230608p18596cdchddcf0a460b79d37f@mail.gmail.com> User-Agent: Mutt/1.5.11 Subject: Re: [PHP-DEV] Fwd: [PHP] proc_open and buffer limit? From: mark@suso.org (Mark Krenz) Thanks for your help. stream_copy_to_stream does seem like a better way to go. However I still have the same problem. Only 65536 bytes are written to /tmp/output.txt. Here is the new source code based on your ideas: -------------------------------------------------------------------------- $program = "/usr/bin/dd of=/tmp/output.txt"; $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("file", "/tmp/error-output.txt", "a") ); $cwd = '/var/www'; $env = array('HOME' => '/var/www'); $process = proc_open($program, $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { stream_set_blocking($pipes[0], FALSE); stream_set_blocking($pipes[1], FALSE); $handle = fopen("/usr/share/dict/words", "r"); stream_copy_to_stream($handle, $pipes[0]); fclose($pipes[0]); fclose($handle); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); print "
$output


\n"; $return_value = proc_close($process); echo "command returned $return_value\n"; } -------------------------------------------------------------------------- I switched to using dd instead of cat because using cat made the situation more complex (because it sends back data as it gets it). The program I'm going to eventually do this with doesn't do that. On Mon, Jan 23, 2006 at 02:08:55PM GMT, Wez Furlong [kingwez@gmail.com] said the following: > 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 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 "
$output


\n"; > > > > $return_value = proc_close($process); > > > > echo "command returned $return_value\n"; > > } > -- Mark S. Krenz IT Director Suso Technology Services, Inc. http://suso.org/