简体   繁体   中英

Show real time wget output in a web page


I'm calling a shell script using shell_exec function of php where I'm downloading a file from another server to the server where this php page is hosted using wget command and sending a report to my page after completing the process. It works fine.

Here is my TestFile.php file

<?php
    $output = shell_exec("sudo /home/user/myScript");
    echo "<pre>$output</pre>";
?>

And my bash script myScript

#!/bin/sh
sudo wget -O /path/to/download/downloadfile http://example.com/TestFile.zip

But I want to show the real time progress of the wget operation on the webpage from where I'm calling the shell script. I want to show just the real time progress in percentage. On this way I'm able to send the progress of wget just in percentage to the webpage using this command

sudo wget -O /path/to/download/downloadfile http://example.com/TestFile.zip 2>&1 | grep -o "[0-9]\+%"

And, The output is like this

2%
4%
6%
8%
...
...
...
95%
97%
99%
100%

But this output is coming after completing the process. At the time of the process running the window doesn't show anything. But I don't want this.

I want the real time update of the process at the time of wget process running not after completing the command.

Could any one help me to solve the problem.

Thanks in advance.

Can't test it at the moment but something like this should work:

<?php

ob_start();

$descriptors = [
    ['pipe', 'r'],
    ['pipe', 'w'],
    ['pipe', 'w'],
];

$process = proc_open('wget blahblah', $descriptors, $pipes, '/tmp', []);

if(is_resource($process)) {
    while ($line = fgets($pipes[1])) {
        echo $line;
        ob_flush();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM