Issues

ZF-8372: Zend_File_Transfer_Adapter_Abstract::getMimeType throws exception File '' does not exist

Description

I have a multi file form element with 3 optional file upload elements, when only 1 or 2 of them are filled by the user, the method Zend_File_Transfer_Adapter_Abstract::getMimeType() will throw a "File '' does not exist" exception on the not filled file elements. The correct behavior should be (as with getFileName and getValue (on the form element) to return an array with just 1 or 2 elements.

Comments

The solution should be to wrap all code within the foreach statement into an if statement:


foreach($files as $key => $value) {
    if (!empty($value['name'])) {
        ...
        original code
        ...
    }
}

When you are calling getMimeType() for 3 elements, then all elements will be checked. When there is no file available but you still requested it, it's definetly an error. The exception is in this case the correct behaviour.

Note that an upload could also have no 'name' assigned. There would be no way to differ between an upload without the 'name' attribute or an empty form field.

Note that Zend_File has no connection to Zend_Form. There is no way to recognise if this element is optional or not.

Situation; I have a multi file form element.


$this->addElement('file', 'attachments', array(
    'label'      => 'Attachment(s) (max. 400KB each)',
    'required'   => false,
    'validators' => array(
        array('count', false, array('max' => 3)),
        array('size', false, 400*1024)),
    'maxFileSize' => 400*1024,
    'multiFile' => 3,
    'destination' => WEBSITE_PATH . 'upload',
));

The user fills in 2 out of 3 available file uploads. During processing the form, I call the methods form the file transfer object via the file form element methods:


$element = $this->getElement('attachments');
$pathNames = $element->getFileName();
$fileNames = $element->getValue();
$mimeTypes = $element->getMimeType();

getFileName() and getValue() return an array with 2 values, but getMimeType() throws an error. This definitely is an error, either in Zend_Form or in Zend_File_Transfer. If Zend_File_Transfer_Adapter_Abstract handles empty names correctly (Zend_File_Transfer_Adapter_Abstract::getFileName() returns just an array of 2 elements) then Zend_File_Transfer_Adapter_Abstract::getMimeType should do the same. So I opt for an error in Zend_File_Transfer_Adapter_Abstract.

Yet another solution would then be to take 'type', 'size' and/or 'tmp_name' into account as well, something like:


foreach($files as $key => $value) {
    if (!empty($value['type']) {
       $result[$key] = $value['type'];
    }
    else if (!empty($value['name']) || !empty($value['tmp_name']) || $value['size'] > 0)) {
        ...
        original code
        ...
    }
}

Having had yet another look at the code, I think that taking the 'ignoreNoFile' option into account might even be better to decide whether to ignore this entry or not.

New feature implemented with r19216