Reputation: 884
My code:
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
function show_status($done, $total, $size=30) {
static $start_time;
if($done > $total) return;
if(empty($start_time)) $start_time=time();
$now = time();
$perc=(double)($done/$total);
$bar=floor($perc*$size);
$status_bar="[";
$status_bar.=str_repeat("=", $bar);
if($bar<$size){
$status_bar.=">";
$status_bar.=str_repeat(" ", $size-$bar);
} else {
$status_bar.="=";
}
$disp=number_format($perc*100, 0);
$status_bar.="] $disp% $done/$total";
@$rate = ($now-$start_time)/$done;
$left = $total - $done;
$eta = round($rate * $left, 2);
$elapsed = $now - $start_time;
$status_bar.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec.";
echo "$status_bar\r";
flush();
if($done == $total) {
echo "\n";
}
}
The call is simply show_status($count, $total_count); - its not working on my server for some reason, but I know the code above works (came from the net, and Ive used it before on another server). Problem Im having now on my current server is that its not outputting it per execution, only seeing the end result (100%) once its processed everything.
What am I missing!? This isnt a browser issue because once again, Im running this via the CLI! PS: Im running this on WAMP with php 5.3.6 with apache 2.2.16
Upvotes: 1
Views: 1040
Reputation: 2669
After looking into the script I believe there is nothing wrong with it. After running this test I was presented with a single line showing me the status at 8% complete:
show_status(10, 1000);
show_status(40, 1000);
show_status(80, 1000);
echo "\n";
However if I run this script:
show_status(10, 1000);
sleep(5);
show_status(40, 1000);
sleep(5);
show_status(80, 1000);
echo "\n";
The output line is periodically REPLACED ever 5 seconds with status 1%, 4%, 8%
I imagine what ever you are tracking the status of is simple completing far faster than the output is rendering and so you are seeing a completed result.
flush() has no effect on CLI.
Upvotes: 2