File Upload Handlers

Introduction

Zend\ProgressBar\Upload provides handlers that can give you the actual state of a file upload in progress. To use this feature you need to choose one of the upload progress handlers (APC, uploadprogress, or Session) and ensure that your server setup has the appropriate extension or feature enabled. All of the progress handlers use the same interface.

When uploading a file with a form POST, you must also include the progress identifier in a hidden input. The File Upload Progress View Helpers provide a convenient way to add the hidden input based on your handler type.

Methods of Reporting Progress

There are two methods for reporting the current upload progress status. By either using a ProgressBar Adapter, or by using the returned status array manually.

Using a ProgressBar Adapter

A Zend\ProgressBar adapter can be used to display upload progress to your users.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$adapter  = new \Zend\ProgressBar\Adapter\JsPush();
$progress = new \Zend\ProgressBar\Upload\SessionProgress();

$filter   = new \Zend\I18n\Filter\Alnum(false, 'en_US');
$id       = $filter->filter($_GET['id']);

$status   = null;
while (empty($status['done'])) {
    $status = $progress->getProgress($id);
}

Each time the getProgress() method is called, the ProgressBar adapter will be updated.

Using the Status Array

You can also work manually with getProgress() without using a Zend\ProgressBar adapter.

The getProgress() will return you an array with several keys. They will sometimes differ based on the specific Upload handler used, but the following keys are always standard:

  • total: The total file size of the uploaded file(s) in bytes as integer.
  • current: The current uploaded file size in bytes as integer.
  • rate: The average upload speed in bytes per second as integer.
  • done: Returns TRUE when the upload is finished and FALSE otherwise.
  • message: A status message. Either the progress as text in the form “10kB / 200kB”, or a helpful error message in the case of a problem. Problems such as: no upload in progress, failure while retrieving the data for the progress, or that the upload has been canceled.

All other returned keys are provided directly from the specific handler.

An example of using the status array manually:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// In a Controller...

public function sessionProgressAction()
{
    $id = $this->params()->fromQuery('id', null);
    $progress = new \Zend\ProgressBar\Upload\SessionProgress();
    return new \Zend\View\Model\JsonModel($progress->getProgress($id));
}

// Returns JSON
//{
//    "total"    : 204800,
//    "current"  : 10240,
//    "rate"     : 1024,
//    "message"  : "10kB / 200kB",
//    "done"     : false
//}

Standard Handlers

Zend\ProgressBar\Upload comes with the following three upload handlers:

APC Progress Handler

The Zend\ProgressBar\Upload\ApcProgress handler uses the APC extension for tracking upload progress.

Note

The APC extension is required.

This handler is best used with the FormFileApcProgress view helper, to provide a hidden element with the upload progress identifier.

Session Progress Handler

The Zend\ProgressBar\Upload\SessionProgress handler uses the PHP 5.4 Session Progress feature for tracking upload progress.

Note

PHP 5.4 is required.

This handler is best used with the FormFileSessionProgress view helper, to provide a hidden element with the upload progress identifier.

Upload Progress Handler

The Zend\ProgressBar\Upload\UploadProgress handler uses the PECL Uploadprogress extension for tracking upload progress.

Note

The PECL Uploadprogress extension is required.

This handler is best used with the FormFileUploadProgress view helper, to provide a hidden element with the upload progress identifier.

Table Of Contents

Previous topic

Progress Bars

Next topic

Introduction to Zend\Serializer

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to File Upload Handlers on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.