AJAX Progress: Reading Output from the Shell

Purpose : to create a progress bar in which users can check how many files were downloaded by my server.

The script . I have a PHP script that runs a python script through popen. I did it like this:

$handle = popen('python last', 'r'); $read = fread($handle, 4096); pclose($handle); 

This python script outputs something like this to the shell:

 [last] ZVZX-W3vo9I: Downloading video webpage [last] ZVZX-W3vo9I: Extracting video information [download] Destination: myvideo.flv [download] 9.9% of 10.09M at 3.30M/s ETA 00:02 

Problem . When I read the file generated by shell output, I get all shell output except the last line !? Why?

Just add, when I run the command through the shell, the shell cursor appears at the end of this line and waits until the script is executed.

Thank you all

+1
source share
3 answers

The first thing that comes to my mind: perhaps the program detects that it is not running on TTY and therefore does not display the last line, which is probably associated with ugly control characters, because this line seems to be updating itself?

What happens when you redirect output to a file (in the shell) or trace it less? If you do not see the last line there, it will most likely be. I do not know of any other solution than to fix the source.

+3
source

Do you read before EOF?

 $handle = popen('python last', 'r'); $read = ""; while (!feof($handle)) { $read .= fread($handle, 4096); } pclose($handle); 
0
source

This will be your friend:

 $handle = popen('python last 2>&1', 'r'); 

Further information can be found here: Wikipedia

0
source

All Articles