IMarks
IMarks

Reputation: 197

Zend form multiple file upload not working

I want to create a image upload to upload multiple images, as many as the user requires.

This is what i have so far.

$this->addElement('file', 'images',
    array(
        'label'         => $this->getView()->Translate('This_Label'),
        'valueDisabled' => true,
        'multiple'      => true,
        'validators'    => array(
            ['Extension', false, 'jpg,png,gif'],
        )
    )
);

However even though the element allows multiple the upload still recieves only one. On the persist my code is as follow, but still i only get the last file debugged.

$this->setEnctype(Zend_Form::ENCTYPE_MULTIPART);

$upload = new Zend_File_Transfer_Adapter_Http();
$files  = $upload->getFileInfo();
foreach ($files as $file => $fileInfo){
    if ($upload->isUploaded($file)) {
        if ($upload->isValid($file)) {
            if ($upload->receive($file)) {
                $info = $upload->getFileInfo($file);
                $tmp  = $info[$file]['tmp_name'];
                Zend_Debug::dump($info, $tmp);
            }
        }
    }
}

How can i make the element upload multiple files? I want a native Zend_Form element, but dont want a repeated element (like the param multiFile does).

Upvotes: 1

Views: 878

Answers (1)

IMarks
IMarks

Reputation: 197

Found the solution, it wasn't that much related to Zend itself. The issue lay in the element not being images[].

So the solution was to add the attrib isArray = true as followed:

$this->addElement('file', 'images',
    array(
        'label'         => $this->getView()->Translate('This_Label'),
        'valueDisabled' => true,
        'isArray'       => true,
        'multiple'      => true,
        'validators'    => array(
            ['Extension', false, 'jpg,png,gif'],
        ),
    )
);

Upvotes: 2

Related Questions