Uploading multiple files using Zend framework- revised
Posted by Faheem Abbas on November 26, 2008
In my previous post I discuss, how to upload multiple files. Sorry for that because I didn’t check the method. And I haven’t tested that on my system and don’t know whether it will be working or not. However All other example I provided in my articles are tested and working fine.
To find an appropriate way of uploading file, I have done it with other method. This is quite helpful and easy.
Fisrt create your form as
class forms_SimpleForm extends Zend_Dojo_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setMethod(‘post’);
$this->setAttrib(‘enctype’, ‘multipart/form-data’);$file1= $this->createElement(‘file’,'file1′);
$file1->setLabel(‘File1:’);$file2= $this->createElement(‘file’,'file2′);
$file2->setLabel(‘File2:’)
->addValidator(’size’,'10′);$submit=$this->createElement(’submit’,’submit’);
$this->addElements(array(
$file1,
$file2,
$submit
));
}
}
?>
The code above is very simple.
We are first creating two file fields and a submit button and then add It to the form using addElements method.
Next in the controller/Action wirte code as
if($this->getRequest()->isPost()){
if($form->isValid($_POST)) {
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator(‘Size’, false, 20000, ‘file2′);
$upload->setDestination(‘tmp/’);
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {
if($upload->isValid($file)){
$upload->receive($file);
}
}
}
}
In the above code we first check whether form is posted and valid. if yes then
We create adapter. Add validator to the file2 and set destination directory.
Next we get info by using $files = $ adapter ->getFileInfo(); using foreach loop and check each file. If file is valid then we upload it other wise leave ignore it.
You can even show error messages if you like. However I am not going to descuss how to handle and show error messages.
Kapricorn said
This does not work. You are just creating an adapter with no link to the form. How do you expect the adapter to accept the files from the form if you have not sent the files to the adapter? The form will simply use its own internal adapter, upload the first file, then error on the second with an illegel operation message.
Please take another look at this and see where you went wrong.
dayg said
Thanks for the tutorial. In case you encounter problems with large file uploads in Zend, this post come in handy: http://dayg.slingandstoneweb.com/2009/02/03/zend-framework-large-file-upload-issues/
Hope this helps.
Faheem Abbas said
Read this one, it will definitely help you.
http://zendguru.wordpress.com/2009/04/16/zend-framework-uploading-and-renaming-multiple-files/
Talib Aziz said
Hello sir,
I am working as developer and using zend framework,
i have read your article about uploading multiple file, but here is problem you have defined the number of file.
I want to use only single file element to upload multiple file and all the files should be displayed besides the element with path what i have selected to upload and after that when i clicked submit button all the file should be uploaded.
please answer me as soon as possible
Talib Aziz
New Delhi.