By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,198 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,198 IT Pros & Developers. It's quick & easy.

proc_open and blocking fread / tri-directional pipes

P: n/a

I would like to call a unix shellscript from within a PHP script and
- write data to its STDIN
- read data from its STDOUT *and* STDERR
- get its exit code afterwards

proc_open seems to be the right thing to use but I have the problem that the called program gives >8kb data on both stdout/stderr back which causes my PHP script to simply hang in the fread call. To be precise the first 4096 "O" characters are read and displayed.

The problem is described in the first user comment to proc_open at but the solution there writes stderr to a file (which never blocks) and therefore does not work for me.

Does anybody know a solution? stream_set_blocking sounds good but
simply calling it with $pipes[1] and $pipes[2] as "stream" argument does not have the wished effect.

bye & TIA,


My test program:

$pipe_spec = array(
0 => array("pipe", "r"), # stdin
1 => array("pipe", "w"), # stdout
2 => array("pipe", "w") # stderr
$process = proc_open("/tmp/testshellskript", $pipe_spec, $pipes);

# stdin
if (fclose($pipes[0]) == 0) trigger_error("fclose0", E_USER_ERROR);

# stdout
while ( ! feof($pipes[1])) print fread($pipes[1], 8192);
if (fclose($pipes[1]) == 0) trigger_error("fclose1", E_USER_ERROR);

# stderr
if (fclose($pipes[2]) == 0) trigger_error("fclose2", E_USER_ERROR);

And the called shell script (writes the chars each 7000 times):

print(STDOUT ("O"x7000)."\n");
print(STDERR ("E"x7000)."\n");
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.