tags:

views:

1667

answers:

6

I'm sure this has been asked before, but as I can't seem to find a good answer, here I am, asking... again. :)

Is there any way, using only a mixture of HTML, JavaScript/AJAX, and PHP, to report the actual progress of a file upload?

In reply to anyone suggesting SWFUpload or similar:

I know all about it. Been down that road. I'm looking for a 100% pure solution (and yes, I know I probably won't get it).

A: 

I'd recommend you to five FancyUpload a try it's a really cool solution for progress bar and it's not necesarely attached to php. Checkout also the other tools at digitarald.de

cheers

PERR0_HUNTER
+1  A: 

Is there any way, using only a mixture of HTML, JavaScript/AJAX, and PHP, to report the actual progress of a file upload?

I don't know of any way to monitor plain HTML (multipart/form-data) file uploads in webserver-loaded PHP.

You need to have access to the progress of the multipart/form-data parser as the data comes in, but this looks impossible because the ways of accessing the HTTP request body from PHP ($HTTP_RAW_POST_DATA and php://input) are documented as being “not available with enctype="multipart/form-data"”.

You could do a script-assisted file upload in Firefox using an upload field's FileList to grab the contents of a file to submit in a segmented or non-multipart way. Still a bunch of work to parse though.

(You could even run a PHP script as a standalone server on another port just for receiving file uploads, using your own HTTP-handling code. But that's a huge amount of work for relatively little gain.)

bobince
There is a PECL extension that does exactly what is needed.
Jacco
A: 

IMHO, this is the problem that Web browsers should solve. We have progress meter for downloads, so why not for uploads as well?

Take a look at this for example:

http://www.fireuploader.com/

Milan Babuškov
If you could include this functionality in all mayor browsers for us, it would be a very nice solutions. Until then, we will just have to work with the current limitations and build the functionality ourselves.
Jacco
+1  A: 

If you're able to add PECL packages into your PHP, there is the uploadprogress package.

The simplest way would be to just use swfupload, though.

kkyy
I've marked this as the answer, because the simplest way IS to just use SWFUpload. :)
abrahamvegh
the simpelest yes, but I think it does not qualify as "pure PHP/AJAX"? :)
Jacco
+2  A: 

If you have APC installed (and by this point, you really should; it'll be standard in PHP6), it has an option to enable upload tracking. There's some documentation, and Rasmus has written a code sample that uses YUI.

R. Bemrose
This would be perfect, except it's not installed my server, and it's not thread safe, so there's no point really. Maybe in PHP6. ;)
abrahamvegh
+5  A: 

Monitoring your file uploads with PHP/Javascript requires the PECL extension:

uploadprogress

A good example of the code needed to display the progress to your users is:

Uber Uploader

If I'm not mistaken it uses JQuery to communicate with PHP.


You could also write it yourself, It's not that complex.

Add a hidden element as the first element of upload form, named UPLOAD_IDENTIFIER.

Poll a PHP script that calls uploadprogress_get_info( UPLOAD_IDENTIFIER ) It return an array containing the following:

time_start     - The time that the upload began (unix timestamp),
time_last      - The time that the progress info was last updated,
speed_average  - Average speed in bytes per second,
speed_last     - Last measured speed in bytes per second,
bytes_uploaded - Number of bytes uploaded so far,
bytes_total    - The value of the Content-Length header sent by the browser,
files_uploaded - Number of files uploaded so far,
est_sec        - Estimated number of seconds remaining.

Let PHP return the info to Javascript and you should have plenty of information. Depending on the audience, you will likely not use all the info available.

Jacco