Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload progress using pure PHP/AJAX?

Tags:

ajax

php

upload

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

like image 482
Abraham Vegh Avatar asked Mar 17 '09 04:03

Abraham Vegh


1 Answers

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.

like image 81
Jacco Avatar answered Sep 22 '22 00:09

Jacco