Symfony 2.1 validator does not see mime-type

眉间皱痕 提交于 2020-01-03 17:18:05

问题


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.



来源:https://stackoverflow.com/questions/12896222/symfony-2-1-validator-does-not-see-mime-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!