Sessions
在线手册:中文  英文

Session Upload Progress

When the session.upload_progress.enabled INI option is enabled, PHP will be able to track the upload progress of individual files being uploaded. This information isn't particularly useful for the actual upload request itself, but during the file upload an application can send an POST request to a separate endpoint (via XHR for example) to check the status.

The upload progress will be available in the $_SESSION superglobal when an upload is in progress, and when POSTing a variable of the same name as the session.upload_progress.name INI setting is set to. When PHP detects such POST requests, it will populate an array in the $_SESSION, where the index is a concatenated value of the session.upload_progress.prefix and session.upload_progress.name INI options. The key is typically retrieved by reading these INI settings, i.e.

<?php
$key 
ini_get("session.upload_progress.prefix") . ini_get("session.upload-progress.name");
var_dump($_SESSION[$key]);
?>

It is also possible to cancel the currently in-progress file upload, by setting the $_SESSION[$key]["cancel_upload"] key to TRUE. When uploading multiple files in the same request, this will only cancel the currently in-progress file upload, and pending file uploads, but will not remove successfully completed uploads. When a upload is cancelled like this, the error key in $_FILES array will be set to UPLOAD_ERR_EXTENSION.

The session.upload_progress.freq and session.upload_progress.min_freq INI options control how frequent the upload progress information should be recalculated. With a reasonable amount for these two settings, the overhead of this feature is almost non-existing

Example #1 Example information

Example of the structure of progress upload array.

<form action="upload.php" method="POST" enctype="multipart/form-data">
 <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
 <input type="file" name="file1" />
 <input type="file" name="file2" />
 <input type="submit" />
</form>

The data stored in the session will look like this:

<?php
$_SESSION
["upload_progress_123"] = array(
 
"start_time" => 1234567890,   // The request time
 
"content_length" => 57343257// POST content length
 
"bytes_processed" => 453489,  // Amount of bytes received and processed
 
"done" => false,              // true when the POST handler has finished, successfully or not
 
"files" => array(
  
=> array(
   
"field_name" => "file1",       // Name of the <input/> field
   // The following 3 elements equals those in $_FILES
   
"name" => "foo.avi",
   
"tmp_name" => "/tmp/phpxxxxxx",
   
"error" => 0,
   
"done" => true,                // True when the POST handler has finished handling this file
   
"start_time" => 1234567890,    // When this file has started to be processed
   
"bytes_processed" => 57343250// Amount of bytes received and processed for this file
  
),
  
// An other file, not finished uploading, in the same request
  
=> array(
   
"field_name" => "file2",
   
"name" => "bar.avi",
   
"tmp_name" => NULL,
   
"error" => 0,
   
"done" => false,
   
"start_time" => 1234567899,
   
"bytes_processed" => 54554,
  ),
 )
);


Sessions
在线手册:中文  英文

用户评论:

Marvin (2013-07-01 20:34:34)

For completion:
Session Upload Progress doesn't work, if session_name is not "PHPSESSID".

Anonymous (2013-06-12 15:34:47)

While the example in the documentation is accurate, the description is a bit off. To clarify:
PHP will populate an array in the $_SESSION, where the index is a concatenated value of the session.upload_progress.prefix and the VALUE of the POSTed session.upload_progress.name variable.

hgs at cs dot columbia dot edu (2013-06-08 21:42:39)

You cannot use the APC RFC1867 and this mechanism on the same server. This mechanism will fail silently if the APC session upload progress is enabled. (See bug #62472).

Anonymous (2013-05-02 08:01:37)

dont't forget, that the session has to be initialized before the form is generated, otherwise the mentioned example above won't work.

isius (2012-09-03 05:11:16)

If you're seeing
"PHP Warning: Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0",
then a misplaced input could be the cause. It's worth mentioning again that the hidden element MUST be before the file elements.

nihaopaul at gmail dot com (2012-08-10 06:12:08)

it should be noted that the hidden element come before the file element otherwise you wont get any updates.

s.zarges (2012-06-19 21:32:42)

Note, this feature doesn't work, when your webserver is runnig PHP via FastCGI. There will be no progress informations in the session array.
Unfortunately PHP gets the data only after the upload is completed and can't show any progress.
I hope this informations helps.

wojbach at o2 dot pl (2012-03-05 12:41:00)

"session.upload-progress.name" -> whether here shouldn't be a underscore instead of a dash

powtac at gmx dot de (2012-03-02 08:35:23)

For PHP >= 5.2 there is http://pecl.php.net/package/uploadprogress

takeone (2011-12-02 07:12:27)

IE6 does not inherit session when you launch new browser from start menu or shortcut.It's the feature.
It is recommended that upload form and progress bar are on same window.

gerd dot randolf at web dot de (2011-11-09 07:29:03)

Note that this will be available from PHP 5.4 on. It won't work in PHP 5.3 or earlier.

易百教程