Writing to stdin from PHP?

眉间皱痕 提交于 2019-12-30 09:00:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5443250/writing-to-stdin-from-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!