Zend Framework Form file component

24 Nov

Zend Framework 1.6 ship file component for uploading file. This component was not available in version 1.5.
Although it has some issues in version 1.6, apologize that I haven’t used this component in version 1.7. So those who are working may find it more mature then version 1.6.
To better illustrate how this component is working lets take a simple example.
The first thing you would need to use this component will definitely be to create

So its very easy to do this in Zend framework, write the following code where you are creating your form.





That’s it, now when you Zend render your form, you will clearly see file field in your form. However don’t forget adding it to your form as


Now the real job start now. You will need to handle your uploading in your controller.
Place the following code in your ControllerAction.

$adapter=new Zend_File_Transfer_Adapter_Http();


'minwidth' => 0,
'maxwidth' => 450,
'minheight' => 0,
'maxheight' => 80)




// code to show errors


If you look at the code above and break it, I think it is simple to understand.
We are first creating instance of the only adapter available in version 1.6. line

$adapter=new Zend_File_Transfer_Adapter_Http();

Create an instance of the http adapter for uploading the file.
Next, we are applying validator to the file. We have define “ImageSize” validator to check the image size. Different type of validators are available and you can easily use them in the way we have used it. See Zend Framework documentation for file valiadors.
The next few line are real magic of zend.


The line above set the destination folder where the file(s) will be uploaded and


is magic method of file component that upload the file(s) and return either true on successful uploading or false on failure.
There are some issues when you want to upload multiple files using this method.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: