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

Issue Type: Improvement Created: 2009-11-23T07:37:42.000+0000 Last Updated: 2009-11-24T12:52:46.000+0000 Status: Resolved Fix version(s): - 1.10.0 (27/Jan/10)

Reporter: Erwin Derksen (erwind) Assignee: Thomas Weidner (thomas) Tags: - Zend_File_Transfer

Related issues: Attachments:


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.


Posted by Erwin Derksen (erwind) on 2009-11-23T07:46:43.000+0000

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

<pre class="highlight">
foreach($files as $key => $value) {
    if (!empty($value['name'])) {
        original code

Posted by Thomas Weidner (thomas) on 2009-11-23T07:58:36.000+0000

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.

Posted by Erwin Derksen (erwind) on 2009-11-23T08:54:03.000+0000

Situation; I have a multi file form element.

<pre class="highlight">
$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:

<pre class="highlight">
$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:

<pre class="highlight">
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

Posted by Erwin Derksen (erwind) on 2009-11-23T09:08:17.000+0000

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.

Posted by Thomas Weidner (thomas) on 2009-11-24T12:52:46.000+0000

New feature implemented with r19216

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.