django
django

Reputation: 2909

PHP progress bar shows only after script completion

I am trying to run example from http://w3shaman.com/article/php-progress-bar-script.

I am using quickPHP on windows. Above tutorial has live Demolink which is running fine.

Problem:

When I test this script, I get bar only when PHP is done processing. I also tried using jQuery UI progress-bar but PHP gets busy before displaying progress bar and then suddenly shows 100 completion at end. What is wrong ?

Code from above link:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;

// Loop through process
for($i=1; $i<=$total; $i++){
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </script>';

    // This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);

    // Send output to browser immediately
    flush();

    // Sleep one second so we can see the delay
    sleep(1);
}

// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</body>
</html>

Upvotes: 2

Views: 3052

Answers (3)

Thupten
Thupten

Reputation: 2218

I had a similar situation at my work. I used jquery, json to check progress of scripts execution. The php script writes a temporary file its status. I blogged about it, there may be some bug here there but does the job quite well.

Upvotes: 0

Aitor Calderon
Aitor Calderon

Reputation: 773

I think right now, QUick PHP doesn't let you get intermediate results as stated here: http://www.zachsaw.com/forum/viewtopic.php?f=11&t=27&hilit=flush (I know it's an old post, but I couldn't find any recent reference to flush use on QuickPHP).

It seems that because of the way QuickPHP handles the output of PHP, it only sends a result when the complete script is done executing. That's why you're getting the bar complete, because your script relies on the possibility to keep sending data to the web browser when the php script is still running.

So the answer is that, it is not possible right now to do it that way.

Upvotes: 1

Red October
Red October

Reputation: 689

Try to use this one:

//At the beginning of your PHP file
ob_start();
...

And then:

ob_flush();
flush();

instead of

flush();

Upvotes: 0

Related Questions