MimeTypes对上传文件Symfony2的限制

时间:2015-01-20 08:55:51

标签: symfony file-upload mime-types

我想限制用户上传。我想提供仅上传pdf格式。我这样做是为了我的领域:

/**
 * @Assert\File(maxSize="6M", mimeTypes={"pdf"})
 */
private $document;

但这没有用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

正确的pdf mime类型为application/pdf - look at this

/**
 * @Assert\File(maxSize="6M", mimeTypes={"application/pdf"})
 */
private $document;

查看official documentation注释部分,也许你遗漏了一些东西

//simple example
namespace Your\Bundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
//test class
class YourClass
{
    /**
     * @Assert\File(
     *     maxSize = "6M",
     *     mimeTypes = {"application/pdf", "application/x-pdf"},
     *     mimeTypesMessage = "Please upload a valid PDF"
     * )
     */
    protected $document;
}