简体   繁体   English

在网页中显示实时wget输出

[英]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. 我正在使用php的 shell_exec函数调用shell脚本,在该脚本中,我将文件从另一台服务器下载使用wget命令托管此php页面的服务器 ,并在完成该过程后向该页面发送报告。 It works fine. 工作正常。

Here is my TestFile.php file 这是我的TestFile.php文件

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

And my bash script myScript 还有我的bash脚本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. 但是我想在调用Shell脚本的地方显示 wget操作的实时进度 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 这样,我可以使用以下命令将wget的进度仅按百分比发送到网页

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. 我不希望在完成命令后立即在运行wget进程时实时更新进程。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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