Uploading multiple files using Zend framework- revised

26 Nov

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.

6 Responses to “Uploading multiple files using Zend framework- revised”

  1. Kapricorn January 2, 2009 at 4:42 pm #

    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.

  2. dayg February 6, 2009 at 4:10 am #

    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.

  3. Faheem Abbas April 16, 2009 at 1:59 pm #

    Read this one, it will definitely help you.
    https://zendguru.wordpress.com/2009/04/16/zend-framework-uploading-and-renaming-multiple-files/

  4. Talib Aziz October 2, 2009 at 11:46 am #

    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.

  5. Emilio July 12, 2010 at 6:25 pm #

    Hello. Thank you for the article. Fixed my problem. Continue like that!😉

  6. satishkandula February 3, 2011 at 12:28 pm #

    i have created 2 boxes by using uploading concept when i upload the pdf,txt files it will enter into box1 and if i upload the jpg type it will enter into 2nd box
    please help me in this

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: