PHP AJAX File Upload Progress Meter Updates
时间:2007-07-20 来源:linxh
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 |
This code only runs when your uploading so you’ll never see the output of it. This can make debugging problematic. One way around this is too hack the UploadProgressMeter to make the iframe non hidden.
Next you setup the JavaScript needed, first HTML_AJAX and then the uploaders JS.
demoserver.php is included in the source, you might want to name it something else if you actually use this code. Its just a page using HTML_AJAX_Server to register the UploadProgressMeterStatus class
You also need some CSS to style the progress bar. The only important thing is that .progressBar and .progressBar .bar are relatively positioned.
Finally you finish things up by building a form. Note that you can include other elements in the form, but if they need to produce output you’ll have to manage calling back into the parent page from the iframes output.