Fun with File Uploads

p. 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”:http://tomas.epineer.se/ and “Joshua Eichorn”:http://blog.joshuaeichorn.com/.

p. Tomas describes his approach in “Asynchronous file upload with AJAX progress bar in PHP”:http://tomas.epineer.se/archives/3 using “Prototype”:http://prototype.conio.net/ combined with PHP to accomplish his effect. Toss in a derivative of “MegaUpload”:http://www.devpro.it/upload_progress/ for server side receiving and monitoring of the upload, and you have a complete package.

p. Going a different route, “Joshua provides PAFUPMU”:http://blog.joshuaeichorn.com/archives/2006/03/14/php-ajax-file-upload-progress-meter-updates/, a set of classes to show upload progress using “HTML_AJAX”:http://htmlajax.org/. 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).

p. Although somewhat different approaches, both methods do agree on using hidden iframes to allow file uploading.

Published: March 14th, 2006 at 1:57
Categories: News
Tags: , , , , , ,

10 comments to “Fun with File Uploads”

Unfortunately a real status-bar is only possible with a patched PHP and if you cannot patch or don’t want to patch the source, there is no real answer to the question, if the file is still being uploaded, you only see a animated .gif assuming there is a real upload going on behind, and that only makes more effort for no use, except the "uploader" seeing a nice animated picture….

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.”

_____anonymous_____
March 16th, 2006 at 9:58 am

As I explain in http://tinyurl.com/s26tn , you can make a progress bar from another window in PHP. Just check for the receiving file at the target folder and monitor its size change. Just a hack, but far more easy than patching PHP or recurring to another language. (by alsanan at indiza.com)

the last idea looks very nice! I will think about this … thanks!

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

Best way [in most cases] is to use AJAX to check and update some DIV Element with the new percentage of the uploaded data (with just one easy filesize check)

I’m Tomas Larsson who wrote one of the articles linked to above. Just wanted to clarify a few things as there seems to be some confusion. There is currently no way to create a progress bar in pure PHP. The best you can do is what alsanan does in the solution he links to above, you can report number of bytes uploaded. But there is no way that you can find out the total size of the file being uploaded so you can’t report progress in percentage of total upload size, which is what a progress bar essentially does.
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 knew I understood it right ;) thanks for clarification!
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 …

That’s not good idea to patch PHP, because some users do not have ability to do patch on shared/virtual hosting. As well as, not all users will be able to do that because of lack of skills. Better solution is to use Perl code piece to track actual upload code. Even no AJAX needed. Look at first <a href="http://www.sibsoft.net/xupload.html">non-AJAX upload progress bar</a>.