如何逐一显示PHP中的每个“回声”。相反,直到所有脚本完成为止

时间:2022-12-06 09:02:51

i have a php script that takes some moments to execute. And it does multiple "echos" as the progress is going. This script connects to ftp, deletes all contents and then upload new files. All is working fine and i'm getting the result but only when the script ends.

我有一个PHP脚本需要一些时间来执行。随着进步的进行,它会做多个“回声”。此脚本连接到ftp,删除所有内容,然后上载新文件。一切都工作正常,我得到的结果,但只有当脚本结束。

My output looks like this:

我的输出如下:

Deleting /var/www/index.php ... ok
Deleting /var/www/tempo.php ... ok
Deleting /var/www/indexOLD.php ... ok
//many lines here ...
//...
//...
Done!
Uploading /var/www/index.php ... ok
Uploading /var/www/tempo.php ... ok
Uploading /var/www/indexOLD.php ... ok
//many lines here ...
//...
//...
Done!

This is my client side code:

这是我的客户端代码:

    function atualizar(id) {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            document.getElementById("estado").value =   xmlhttp.responseText;
        };
        xmlhttp.open("GET","atualizarBOX.php?id="+id,true);
        xmlhttp.send();
}

And here is my serverside php piece of code:

这是我的serverside php代码段:

function ftp_putAll($conn_id, $src_dir, $dst_dir) {
$d = dir($src_dir);
while($file = $d->read()) { // do this for each file in the directory
    if ($file != "." && $file != "..") { // to prevent an infinite loop
        if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
            if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {
                ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
            }
            ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
        } else {
            ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
            print "Uploading ".$src_dir."/".$file. " ... OK \n";
        }
    }
}
$d->close();

}

}

I already tried to use ob_flush(); flush(); but it ist working. Also i already changed this values

我已经尝试过使用ob_flush();冲洗();但它正在发挥作用。我也已经改变了这个值

In php.ini:

在php.ini中:

. output_buffering = Off

。 output_buffering =关闭

. zlib.output_compression = Off

。 zlib.output_compression =关闭

All i want is to get "echo" one by one as the progress is going... Something like this:

我想要的就是随着进展的进行逐一“回声”...这样的事情:

Deleting /var/www/index.php ... ok

//now user waits a couple of seconds

Deleting /var/www/index.php ... ok

1 个解决方案

#1


1  

Quick google search reveals that a common way of solving this is called 'comet programming technique'

快速谷歌搜索显示,解决此问题的常用方法称为“彗星编程技术”

Here is a quick PHP example (Source)

这是一个快速的PHP示例(来源)

Stealing server side code from above link and pasting here:

从上面的链接窃取服务器端代码并​​粘贴到这里:

<?php
/**
    Ajax Streaming without polling
*/

//type octet-stream. make sure apache does not gzip this type, else it would get buffered
header('Content-Type: text/octet-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

/**
    Send a partial message
*/
function send_message($id, $message, $progress) 
{
    $d = array('message' => $message , 'progress' => $progress);

    echo json_encode($d) . PHP_EOL;

    //PUSH THE data out by all FORCE POSSIBLE
    ob_flush();
    flush();
}

$serverTime = time();

//LONG RUNNING TASK
for($i = 0; $i < 10; $i++)
{
    //Hard work!!
    sleep(1);

    //send status message
    $p = ($i+1)*10; //Progress

    send_message($serverTime, $p . '% complete. server time: ' . date("h:i:s", time()) , $p); 
}
sleep(1);
send_message($serverTime, 'COMPLETE'); 

Looks like you just need to add the appropriate headers..

看起来你只需要添加适当的标题..

#1


1  

Quick google search reveals that a common way of solving this is called 'comet programming technique'

快速谷歌搜索显示,解决此问题的常用方法称为“彗星编程技术”

Here is a quick PHP example (Source)

这是一个快速的PHP示例(来源)

Stealing server side code from above link and pasting here:

从上面的链接窃取服务器端代码并​​粘贴到这里:

<?php
/**
    Ajax Streaming without polling
*/

//type octet-stream. make sure apache does not gzip this type, else it would get buffered
header('Content-Type: text/octet-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

/**
    Send a partial message
*/
function send_message($id, $message, $progress) 
{
    $d = array('message' => $message , 'progress' => $progress);

    echo json_encode($d) . PHP_EOL;

    //PUSH THE data out by all FORCE POSSIBLE
    ob_flush();
    flush();
}

$serverTime = time();

//LONG RUNNING TASK
for($i = 0; $i < 10; $i++)
{
    //Hard work!!
    sleep(1);

    //send status message
    $p = ($i+1)*10; //Progress

    send_message($serverTime, $p . '% complete. server time: ' . date("h:i:s", time()) , $p); 
}
sleep(1);
send_message($serverTime, 'COMPLETE'); 

Looks like you just need to add the appropriate headers..

看起来你只需要添加适当的标题..