Upload progress using pure PHP/AJAX?

后端 未结 6 878
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题