Often for long running tasks you want to show progress to users, at least to indicate your application is still running.

As PHP is a programming language at server side, it appears difficult to communicate with client to update status of jobs running on server.

With a little help of AJAX and Javascript, below is an example to show how you can update a progress bar for a long running task. The solution is built on PHP framework CodeIgnitor but the same concept can be used in pretty much any Model/View/Controller frameworks.

  1. In your PHP application, in your view file, put a progress bar (id=”progressor”)

<progress id=”progressor” value=”0″ max=’100′ ></progress>

  1. In same view file, put the following Javascript code. I’ll explain the code later


<script type=”text/javascript”>

function showStatus() {

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() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

document.getElementById(“progressor”).value = xmlhttp.responseText;









function timer(){


setInterval(function () {showStatus()},1000);




  1. In your PHP controller file, create the following functions to help show status


public function show_progressbar()


$percent = util_read_progress();

echo $percent;



//write to text file for progress bar

function util_write_progress($percent)


$filename = “tmp_progress_bar.txt”;

$fp = fopen($filename,”w”);





function util_read_progress()


$filename = “tmp_progress_bar.txt”;

if (file_exists ($filename) ) {

$content = file($filename);

//put content in array

$percent = $content[0];

} else {

$percent = 0 ;


return $percent;



  1. In your long running controller PHP code, you can use function util_write_progress to put progress percentage in:

time_nanosleep(0, 25000000);



So here is how everything works together

  1. Java script is timed to run every second(1000 miliseconds); when it runs, it queries http://yoursite/index.php/emails/show_progressbar to get job’s status(percentage of completion). Once the result is available, it updates the progress bar(document.getElementById(“progressor”).value = xmlhttp.responseText;).
  2. Long running job periodically writes its status to a local temporary file(util_write_progress), which is read by function util_read_progress.
PHP AJAX to generate progress bar

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 7 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.