File uploads and upload progress
- Posted by thomasw at 23:03:22 // //
- Filter
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
Add comment
Fill out the form below to add your own comments

Thursday, March 26, 2009 - 18:52:38
wow that’s a nice addition!
Thursday, April 9, 2009 - 01:16:44
It looks like very intereting, but unfortunately it doesn’t work for me. When I use it after receive file and let it write out variable $upload, it writes out: array(5) {
[”total”] => int(0)
[”current”] => int(0)
[”rate”] => int(0)
[”message”] => string(21) “No upload in progress”
[”done”] => bool(true)
}
Could you advice me?
Thursday, April 9, 2009 - 01:23:08
And how should it work after you already received the file ?
There is no upload in progress when the file is already transferred to the server.
You should look at the example which is provided within Zend Framework to see how this work in a live envionment.
Thursday, April 9, 2009 - 11:50:04
I know it’s idiocy, I don’t know were I have to placed method recieve, when this method transfer file and then is performed other code.
If you mean Zend_ProgressBar I looked at it before. But this new feature was looks like much easier and focused on uploaded file. In article is writern that only one thing have to do and everything other is going in background.
Do you think about some tutorial on this topic? I have solved many problems of various kinds, but this one I can’t solve a long time.
Thursday, April 9, 2009 - 12:06:58
You should firstly google for the difference between Upload and Progress.
When you’ve ever done a file upload you would know that you can’t get the progress within the same script where you receive the file.
Actually there is a manual which describes how this works, and there is a working example within Zend Framework which practically shows how a implementation is done.
Looking at your comment I expect that you’ve not run the example at all. So as already said before: “You should look at the example which is provided within Zend Framework to see how this work in a live envionment.”
Thursday, April 9, 2009 - 12:18:20
As you may also have noted, this example above does not describe how to view the data to the user, nor does it describe how to work with the data after it has been received.
For how to receive files after they have been transferred look at the other blog entries and the manual. See Zend_File_Transfer
For how to display the data to users, look into Zend_File_Transfer and Zend_Progressbar.
The 2 examples can be found under /demos/Zend/ProgressBar
Upload and ZendForm… first uses manual declaration and second uses Zend_Form.
Thursday, April 9, 2009 - 23:14:23
Where I see problem is that I have almost everything done. Form by Zend_form, upload with validators by Zend_File_Transfer. But what I can’t integrate is some progress bar. I read everything what you recommend and tried use demo named ZendForm.php you talk about.
Function evalProgress used exeption, I tried out that method httpRequest.responseText return whole html code of current page, what I think isn’t correct behavior. What is wrong?
Friday, April 10, 2009 - 00:04:49
Please think logically…
Should I throw the dices ? Or lay cards ? :o
There is nothing where I could see what you’ve done wrong in your code.
As said several times in past: please use the mailing list when you’ve problems with own code. A blog is the wrong channel to do bugfixing. :-)
Friday, April 10, 2009 - 11:38:02
Yes sorry Tomas, I thought if it (returning of contain of whole page) isn’t common thing which is happened, because I met many times with it in past and I didn’t know what that mean.
It was pure desperate though that I’ll get respond on the blog, I made because on this page I found the most relevant and the newest information about progress upload even more than in zend manual and other sources:).
Saturday, June 13, 2009 - 01:20:33
Thomasw, do you have an example of just file upload using the ZendFramework? I am a Flex developer and I want to use the ZendFramework to receive a file (after upload from Flex) and move it to a location on the server. Flex cannot save to the server.
Any suggestions? Just the ZendFramework code I am interested in.
Thanks.
Peter
Monday, August 3, 2009 - 14:40:47
Hey,
Are you going to make this blog useful or just repeat lines of code from the documentation?
Monday, August 3, 2009 - 16:16:05
Just because I write things first in my blog and after that into the manual does not mean that these things are not usefull.
When you don’t like what I write privatly in my sparetime then feel free not to read it. It’s just my private blog and no one forced you to read it ;-)
Thursday, February 11, 2010 - 14:57:53
Hallo Thomas,
there is also one bug in the ZendForm.php Demo of Zend_Progressbar. Because of this the dome can’t work.
You must add on Line 196 ( $form = new Zend_Form….. ) the following element :
array(’progress_key’ => array(’hidden’, array(’value’ => md5(uniqid(rand())))))
otherwise the javascript throws an error ;)
can you make an example of a working file upload with the Zend_Progressbar?
Thursday, February 11, 2010 - 16:04:54
The progressKey is created by the file element itself.
When you add it manually it will not work anymore.
There is only one reason why the progressKey is not created automatically… when your installation does not support progress at all. In this case adding the key manually would not help as there is no progress to display at all.
Also to note: The progressbar demo is not from me because the progressbar component is not from me.
Friday, October 15, 2010 - 03:17:47
Hey,
how can I implement server side validation if my form needs to target a hidden iframe?
The progress bar is working fine, but as long as the server response to the post request (including possible validation messages) ends up in a hidden frame, the approach used in the zf demos is not really practical.
Or am I terribly mistaken here?
Wednesday, March 28, 2012 - 18:47:44
Helpful information. Lucky me I discovered your website by chance, and I’m surprised why this twist of fate didn’t came about earlier! I bookmarked it.