Upload Progress Bar in PHP

前端 未结 10 1470
栀梦
栀梦 2020-11-22 17:33

Does anyone know how to get a progress bar for an upload in php? I am trying writing code for a photo album uploader. I would like a progress bar to display while the photos

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 17:49

    Implementation of the upload progress bar is easy and doesn't require any additional PHP extension, JavaScript or Flash. But you need PHP 5.4 and newer.

    You have to enable collecting of the upload progress information by setting the directive session.upload_progress.enabled to On in php.ini.

    Then add a hidden input to the HTML upload form just before any other file inputs. HTML attribute name of that hidden input should be the same as the value of the directive session.upload_progress.name from php.ini (eventually preceded by session.upload_progress.prefix). The value attribute is up to you, it will be used as part of the session key.

    HTML form could looks like this:

    When you send this form, PHP should create a new key in the $_SESSION superglobal structure which will be populated with the upload status information. The key is concatenated name and value of the hidden input.

    In PHP you can take a look at populated upload information:

    var_dump($_SESSION[
        ini_get('session.upload_progress.prefix')
       .ini_get('session.upload_progress.name')
       .'_myupload'
    ]);
    

    The output will look similarly to the following:

    $_SESSION["upload_progress_myupload"] = array(
      "start_time" => 1234567890,   // The request time
      "content_length" => 57343257, // POST content length
      "bytes_processed" => 54321,   // Amount of bytes received and processed
      "done" => false,              // true when the POST handler has finished, successfully or not
      "files" => array(
        0 => array(
          "field_name" => "file1",    // Name of the  field
          // The following 3 elements equals those in $_FILES
          "name" => "filename.ext",
          "tmp_name" => "/tmp/phpxxxxxx",
          "error" => 0,
          "done" => false,            // True when the POST handler has finished handling this file
          "start_time" => 1234567890, // When this file has started to be processed
          "bytes_processed" => 54321, // Number of bytes received and processed for this file
        )
      )
    );
    

    There is all the information needed to create a progress bar — you have the information if the upload is still in progress, the information how many bytes is going to be transferred in total and how many bytes has been transferred already.

    To present the upload progress to the user, write an another PHP script than the uploading one, which will only look at the upload information in the session and return it in the JSON format, for example. This script can be called periodically, for example every second, using AJAX and information presented to the user.

    You are even able to cancel the upload by setting the $_SESSION[$key]['cancel_upload'] to true.

    For detailed information, additional settings and user's comments see PHP manual.

提交回复
热议问题