Upload progress using pure PHP/AJAX?

孤街醉人 提交于 2019-12-17 15:45:12

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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




回答6:


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/



来源:https://stackoverflow.com/questions/653063/upload-progress-using-pure-php-ajax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!