简体   繁体   中英

How to show each "echo" from PHP one by one. Instead wainting until the all script is complete

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.

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:

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

In php.ini:

. output_buffering = Off

. zlib.output_compression = Off

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

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

Here is a quick PHP example (Source)

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..

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