Zend\Validator\File\MimeType raises a warning if no file was uploaded #5105

Closed
liorch88 opened this Issue Sep 12, 2013 · 3 comments

Projects

None yet

3 participants

@liorch88

When I am using the Zend\Validator\File\MimeType validator (actually, the IsImage validator) on a form, if no file was uploaded - it will raise the following warning:

Warning: finfo_file(): Empty filename or path in \vendor\zendframework\zendframework\library\Zend\Validator\File\MimeType.php on line 392

The file field is created using the following annotation:

    /**
     * @Annotation\Type("Zend\Form\Element\File")
     * @Annotation\Attributes({"accept":"image/*"})
     * @Annotation\Required({"required":"true"})
     * @Annotation\Validator({"name": "File\UploadFile"})
     * @Annotation\Validator({"name": "File\IsImage"})
     * @Annotation\Options({"label":"Image:"})
     */
    protected $image;

And the form was created in the way the manual suggests:

// Merge post and file info
 $post = array_merge_recursive(
     $request->getPost()->toArray(),
     $request->getFiles()->toArray()
);


$form->setData($post);

if ($form->isValid()) {
    //...
}
@ThaDafinser
Contributor

@liorch88 you are doing it general right.

In my code (multiple upload enabled), i check for an empty file name an skip it.

Can u check if this "hack" would solve your problem? I think the problem have to fixed elsewhere and not in the MimeType....

            $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
            $form->setData($post);
            if ($form->isValid() === true) {
                $files = $request->getFiles()->toArray();

                $imageFiles = $files['images'];
                foreach ($imageFiles as $imageFile) {
                    if ($imageFile['name'] == '') {
                        continue;
                    }

                    //ok...do something with the file
                }
@liorch88
liorch88 commented Nov 1, 2013

I believe the validator itself should be able to handle something as common as a missing file - because it is a very common situation that a user simply did not upload a file.

I think the solution should be in MimeType. Before running the actual mimetype checks, it should see that the $file is ok. Maybe just before the first check (the "is file name readable? check), around line 370, to add something like:

// File name not empty?
if (empty($file)) {
    $this->error(static::NOT_READABLE);
    return false;
}

The other reason for doing so in the validator itself, and not on the user side - is that I believe that the framework itself should never cause an "uncaught" error or warning - it should try and handle all the php errors itself (as it does using the ErrorHandler..)

@GeeH GeeH added the To Be Closed label Mar 5, 2016
@GeeH
GeeH commented Jun 27, 2016

This issue has been closed as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html

@GeeH GeeH closed this Jun 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment