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.
- In your PHP application, in your view file, put a progress bar (id=”progressor”)
<progress id=”progressor” value=”0″ max=’100′ ></progress>
- 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;
//alert(xmlhttp.responseText);
}
}
xmlhttp.open(“GET”,”http://yoursite/index.php/emails/show_progressbar”,true);
xmlhttp.send();
}
function timer(){
//showStatus();
setInterval(function () {showStatus()},1000);
}
</script>
- 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”);
fputs($fp,$percent);
fclose($fp);
}
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;
}
- In your long running controller PHP code, you can use function util_write_progress to put progress percentage in:
time_nanosleep(0, 25000000);
util_write_progress(ceil($current/$count*100));
So here is how everything works together
- 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;).
- Long running job periodically writes its status to a local temporary file(util_write_progress), which is read by function util_read_progress.