Symfony 2.1 validator does not see mime-type

2019-06-26 01:15发布

问题:

I'm working on Symfony 2.1 where i'm adding images to the default fosUserBundle User entity. On top of my entity I have this:

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="users")
*/
class User extends BaseUser

Next the concerning field:

/**
* @ORM\Column(nullable=true)
* @Assert\File(maxSize="5M", mimeTypes={"image/gif", "image/jpeg", "image/png"})
*/
protected $promo;

The error I'm getting is The mime type of the file is invalid (""). Allowed mime types are "image/gif", "image/jpeg", "image/png"..

The weird thing is, when I var_dump $this->promo->getClientMimeType() I neatly get image/jpeg... So for some reason the validator is looking in the wrong location?

Does anybody have any idea what might cause this?

回答1:

As it turns out it is crucial to have the php_fileinfo extension installed in your php. Without it the validator can nog validate your files as it uses the guessExtion() method from uploadedFile.

This fixed my problem. However, on shared hosting this might cause trouble since you might not be able to enable php_fileinfo.