Skip to Content

Progress Bar in PHP - Implement a progress bar with PHP

Almost every PHP developer feels the need to display a progress meter or progress bar in PHP at some point in their life when large amount of data processing is done on the server either in the form of file uploading or bulk insert/update operations of database queries are being performed however not may of them know that PHP has a big limitation that does not allow to access raw data while code execution is being done.

Good news is that we can achieve our goal using PHP?s flushing buffer to create a progress bar for large PHP applications. You can use the flush() function to push more data to the browser while the php script is running using the following code. This data being elements for small pieces of the progress bar, you can rather easily have a universal solution for all heavy scripts.

ob_end_flush(); // This is to clear the buffer and it should be called at start
?>


PHP Progress Bar Example
function DisplayProgress(prog_value,div_id)
{
document.getElementById(div_id).innerHTML = prog_value + "% done out of 100%";
}
-->







$total_count = 100000;
for($i=0; $i {
$progress_made = ($i/$total_count)*100;
print "n";
flush(); // Push the new data to browser
}
?>

Please note that on some occasions, the webserver, proxy or the client browser can buffer data no matter what you do, so this will not work 100% for everyone at every situation. Still, this is a great idea.

Powered by PHPKB Knowledge Base Software