Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to stdin from PHP?

In linux I want to run a gnome zenity progress bar window from PHP. How zenity works is like this:

linux-shell$ zenity --display 0:1 --progress --text='Backing up' --percentage=0
10
50
100

So the first command opens the zenity progress bar at 0 percent. Zenity then takes standard input numbers as the progress bar percentage (so it will go from 10% to 50% to 100% when you type those numbers in).

I can't figure out how to get PHP to type in those numbers though, I have tried:

exec($cmd);
echo 10;
echo 50;

And:

$handle = popen( $cmd, 'w' );
fwrite( $handle, 10 );

And:

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w")  // stdout is a pipe that the child will write to
);

$h = proc_open($cmd, $descriptorspec, $pipes);

fwrite($pipes[1], 10);

But none of them updates the progress bar. In what way can I mimic the effect of the stdin on the linux shell to get zenity to update its progress bar?

like image 520
hamstar Avatar asked Mar 26 '11 15:03

hamstar


Video Answer


1 Answers

Your first executes the command with a copy of the current script's stdin, not the text you provide.

Your second fails because you are forgetting the newline. Try fwrite($handle, "10\n") instead. Note that zenity seems to jump to 100% when EOF is reached (e.g. by the implicit close of $handle at the end of your PHP script).

Your third fails because you are forgetting the newline and you are writing to the wrong pipe. Try fwrite($pipes[0], "10\n") instead, and remember the same note regarding EOF as above.

like image 174
Anomie Avatar answered Sep 18 '22 07:09

Anomie