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 "<PRE>$output</PRE><BR><BR>\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 [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]> 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";
> > }
> 


-- 
Mark S. Krenz
IT Director
Suso Technology Services, Inc.
http://suso.org/

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to