Tuesday
3
March
2009

File uploads and upload progress

Sometimes it is necessary to show the progress for files which a user actually uploads to a site.
With the next release 1.8 the Zend Framework supports a very easy and simple way to provide the user with all wished data.

There are some prerequisits:
You must have installed and activated eighter the APC or UploadProgress extension.
You should use Zend_Form_File_Element, otherwise the needed hidden fields will not be attached.

So let’s expect you have already created the form and the controller to handle the upload itself.

To show your user the progress of his file upload you need to have an iframe or another different process beside the upload itself which shows it’s result to the user. The server part looks like this:

$adapter = new Zend_ProgressBar_Adapter_Console();
$upload  = Zend_File_Transfer_Adapter_Http::getProgress($adapter);
	
while (!$upload['done']) {
    $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
}

There is only one thing you have to do:
* Create the wished ProgressBar adapter and give it to Zend_File_Transfer_Adapter_Http::getProgress()

All other things are done in background by this static method. You don’t need to create the form, or a file adapter. Simply call this static method.

In return your ProgressBar adapter will be updated and will show you the new progress for this upload.
However you realize your view, there are different methods to do so, you have to give the output of Zend_File_Transfer_Adapter_Http::getProgress() as input to the next call of this method.

It was never simpler to get the upload progress and show it to a user.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top