Over the weekend my file upload progress meter code got lots of traffic. It seems it made it made it on the
del.icio.us popular list as well as getting over a thousands diggs. To celebrate this i’ve updated the code.
The main new feature is giving you feedback without having to patch PHP. Now the patched version gives you more information such as upload speed and estimated time to completion. But we still provide some nice user feedback even without it now.
I also created some wiki pages to start the documentation process.
So here are the demo’s
With the patch and extension
Without the patch
To get upload speed you will need to download and install the PHP Upload Progress Patch and extension.
If you want to use the code you need to:
Install HTML_AJAX (pear install HTML_AJAX-alpha)
Download PAFUPMU and install it somewhere accessible.
Add the code to your page using demo.php as an example.
The basic way the code works is we take a form containing a file upload and submit it using a hidden iframe as a target. Doing this lets the upload happen in the background. Then we poll the server on a regular basis (say a 2 second interval) asking it for update status. If you have the patch+extension installed this gives you file upload speed etc. If you don’t have the extesion we stop making AJAX calls and just animate the status bar until the upload finishes in the iframe and tells us were done.
In demo.php we handle generating the current page and handling the upload in the same page. So we start by handling the update.
<?php
include 'UploadProgressMeter.class.php';
$fileWidget = new UploadProgressMeter();
if ($fileWidget->uploadComplete()) {
// output javascript to the iframe to send a final status to the main window
// this will catch error conditions
echo $fileWidget->finalStatus();
// move the file(s) where they need to go
exit;
}
?>