Symfony 2.1 验证器看不到 MIME 类型 [英] Symfony 2.1 validator does not see mime-type
问题描述
我正在开发 Symfony 2.1,我正在将图像添加到默认的 fosUserBundle 用户实体.在我的实体之上,我有这个:
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
接下来是相关字段:
/**
* @ORM\Column(nullable=true)
* @Assert\File(maxSize="5M", mimeTypes={"image/gif", "image/jpeg", "image/png"})
*/
protected $promo;
我得到的错误是 文件的 MIME 类型无效 ("").允许的 mime 类型为image/gif"、image/jpeg"、image/png".
.
奇怪的是,当我 var_dump $this->promo->getClientMimeType()
我整齐地得到 image/jpeg
...因此,出于某种原因,验证器正在寻找错误的位置?
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?
推荐答案
事实证明,在您的 php.ini 文件中安装 php_fileinfo
扩展是至关重要的.没有它,验证器可以使用 uploadedFile
中的 guessExtion()
方法来验证您的文件.
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
.
这解决了我的问题.但是,在共享主机上这可能会导致问题,因为您可能无法启用 php_fileinfo.
This fixed my problem. However, on shared hosting this might cause trouble since you might not be able to enable php_fileinfo.
这篇关于Symfony 2.1 验证器看不到 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!