Posted by Faheem Abbas on November 24, 2008
Some developer may find it hard to upload several files using Zend Framework file component. In some cases developers may provide more than one file fields, say five(5) and when user tries to upload three files, so instead of uploading those three files, his files are simply ignored from being uploaded.
This is because Zend Framework uses receive function which first check all the file fields. If any of them is empty or unvalid, Zend Framwork file receive() function stop execution and none of the file uploaded.
A simple solution to this problem is to take file names of each file element and pass it to the receive function like
$adapter->receive(‘filepath’);
Note that receive function take a path to the file in form of string.
Example
Consider you have the following file fields in your form
file1,file2,$file3 etc.
so in your controller/action
$values=$form->getValues();
$file1=$values[‘file1’];
$file2=$values[‘file2’];
$file3=$values[‘file3’];
And so on, the above code will give you paths of each file field. Now
$adapter->receive($file1);
$adapter->receive($file2);
$adapter->receive($file3);
So these three files will be uploaded to the specified directory.
Posted in Zend Framework | 3 Comments »
Posted by Faheem Abbas on November 24, 2008
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.
$file=$this->CreateElement('file','file');
$file->setLabel('File’)
->setRequired(false)
->setAttrib('tabindex','15');
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
$form->addElement($file);
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();
$adapter->addValidator(‘ImageSize’,
array(
'minwidth' => 0,
'maxwidth' => 450,
'minheight' => 0,
'maxheight' => 80)
);
$adapter->setDestination(‘../tmp/’);
if(!$adapter->receive()){
// 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.
$adapter->setDestination(‘../tmp/’);
The line above set the destination folder where the file(s) will be uploaded and
$adapter->receive();
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.
Posted in Zend Framework | Leave a Comment »