Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Progress for file uploads

Zend_File_Transfer can give you the actual state of a fileupload in progress. To use this feature you need either the APC extension which is provided with most default PHP installations, or the UploadProgress extension. Both extensions are detected and used automatically. To be able to get the progress you need to meet some prerequisites.

First, you need to have either APC or UploadProgress to be enabled. Note that you can disable this feature of APC within your php.ini.

Second, you need to have the proper hidden fields added in the form which sends the files. When you use Zend_Form_Element_File this hidden fields are automatically added by Zend_Form.

When the above two points are provided then you are able to get the actual progress of the file upload by using the getProgress() method. Actually there are 2 official ways to handle this.

Using a progressbar adapter

You can use the convinient Zend_ProgressBar to get the actual progress and can display it in a simple manner to your user.

To archive this, you have to add the wished Zend_ProgressBar_Adapter to getProgress() when you are calling it the first time. For details about the right adapter to use, look into the chapter Zend_ProgressBar Standard Adapters.

Example 405. Using the progressbar adapter to retrieve the actual state

= new Zend_ProgressBar_Adapter_Console();
$upload  Zend_File_Transfer_Adapter_Http::getProgress($adapter);

$upload null;
while (!
$upload['done']) {
$upload Zend_File_Transfer_Adapter_Http:getProgress($upload);

The complete handling is done by getProgress() for you in the background.

Using getProgress() manually

You can also work manually with getProgress() without the usage of Zend_ProgressBar.

Call getProgress() without settings. It will return you an array with several keys. They differ according to the used PHP extension. But the following keys are given independently of the extension:

  • id: The ID of this upload. This ID identifies the upload within the extension. You can set it to the value of the hidden key which identified the upload when initially calling getProgress(). Per default it is set to progress_key. You must not change the ID afterwards.

  • total: The total filesize of the uploaded files in bytes as integer.

  • current: The current uploaded filesize 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: The actual message. Either the progress as text in the form 10kB / 200kB, or a helpful message in the case of a problem. Problems could be, that there is no upload in progress, that there was a failure while retrieving the data for the progress, or that the upload has been canceled.

  • progress: This optional key takes a instance of Zend_ProgressBar_Adapter or Zend_ProgressBar and allows to get the actual upload state within a progressbar.

  • session: This optional key takes the name of a session namespace which will be used within Zend_ProgressBar. When this key is not given it defaults to Zend_File_Transfer_Adapter_Http_ProgressBar.

All other returned keys are provided directly from the extensions and will not be checked.

The following example shows a possible manual usage:

Example 406. Manual usage of the file progress


while (!
$upload['done']) {
$upload Zend_File_Transfer_Adapter_Http:getProgress($upload);
"\nActual progress:".$upload['message'];
// do whatever you need

Knowing the file to get the progress from

The above example works when your upload identified is set to 'progress_key'. When you are using another identifier within your form you must give the used identifier as first parameter to getProgress() on the initial call.

Zend Framework