I want to upload multiple images using Zend_Form_Element_File with the HTML attribute multiple. Setting up the element works intuitively:
$form->addElement(new Zend_Form_Element_File(
'images',
array('label' => 'Images:', 'multiple' => 'multiple')
));
When I get a reference to the input field in my upload page und do a $input->receive(), it only saves one of the selected files.
When I search for something like “Zend_Form_Element_File multiple files”, I only get results that show the use of Zend_Form_Element_File::setMultiFile(), which isn’t what I’m looking for.
Any suggestions?
Just found the answer to my own question:
You have to set ‘isArray’ on the input element to true, either in the options array when constructing the element (
new Zend_Form_Input_File('images', array('isArray' => true))) or via the isArray() method ($input->setIsArray(true)).Some background info and how I finally found this solution:
First, I realized that I have to apend square brackets to the element’s name for multiple file uploads to work, e.g.
images[]. I don’t know if that’s the HTML specification or if that’s something PHP requires, but that’s how it should work in this case.My next problem was that
Zend_Form_Elementremoves the brackets when I try to set them with thenameproperty. Finally, I stumbled upon the answer by Matthew Weier O’Phinney himself:isArrayappends the[]for you.