Upload progress using pure PHP/AJAX?

后端 未结 6 852
盖世英雄少女心
盖世英雄少女心 2020-12-05 09:11

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, Java

相关标签:
6条回答
  • 2020-12-05 09:27

    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

    0 讨论(0)
  • 2020-12-05 09:29

    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.

    0 讨论(0)
  • 2020-12-05 09:31

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

    0 讨论(0)
  • 2020-12-05 09:32

    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.

    0 讨论(0)
  • 2020-12-05 09:37

    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.

    0 讨论(0)
  • 2020-12-05 09:39

    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/

    0 讨论(0)
提交回复
热议问题