In good programming languages there is almost always more than one way to accomplish a goal, PHP is no different. Here we have two very different methods for showing an AJAX progress bar during a file upload which were recently discussed by bloggers Tomas Larsson and Joshua Eichorn.
Tomas describes his approach in Asynchronous file upload with AJAX progress bar in PHP using Prototype combined with PHP to accomplish his effect. Toss in a derivative of MegaUpload for server side receiving and monitoring of the upload, and you have a complete package.
Going a different route, Joshua provides PAFUPMU, a set of classes to show upload progress using HTML_AJAX. With any PHP version newer than 4.3.7 or 5.0 it provides similar functionality to Tomas’ solution. For older versions of PHP you either need to add a patch, or end up with a basic animation and no progress bar (reminded me a bit of KIT from Knight Rider, or the Cylon oscillating faceplate, pick one).
Although somewhat different approaches, both methods do agree on using hidden iframes to allow file uploading.


Comments (Login to leave comments)
Markus L. aka ViShap
Markus,
I’ve not delved too deeply into either solution but it is my understanding that this is more than just an animated gif. Both systems work by making regular XHR calls to a server-side piece of code that can check the bytes uploaded. If my understanding is correct, this would give a real upload status-bar without a patch to PHP.
=C=
One solution does not require any patching at all, the other only for older versions of PHP. The patch itself is marked as being deprecated and “no longer needed.”
and to the "no update-progress bar": in the demo its afaics only a gif going from one end to another like Knight Rider's KIT ...
But I didn't look at it in detail, but if you are right, and a progress bar is possible w\o updating this would be very interesting ...
thanks for the info!
regardz markus
There are two ways around this limitation in PHP. One is to patch PHP. This is what Joshua Eichorn does in his solution. Contrary to what's been stated above, you still need to apply the patch even if you have the latest version of PHP. You can verify this by downloading Joshua's solution. It will only give you progress bar if you have patched PHP.
The other way around PHP's limitation is what I use in the solution linked to above. You upload a small perl script to your cgi-bin directory. This perl file checks the total size of the upload and writes it to a file, so it can be read form the AJAX function. Everything else is still handled by PHP.
Hope that clears things up!
Cheers,
tomas
Tomas,
Thanks for the clarification.
=C=
I think the perl-solution is the most accessable for all, because if you use cpanel or sth on your server, patching php could become a problem ...