Laravel验证者哑剧规则不适用于文档

我正尝试将文档文件上传为pdf,doc,docx。 为了让我使用这个值:

$mimes = [
      'doc' => [
        'application/msword',
        'application/vnd.ms-office'
      ],
      'docx' => [
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
      ],
      'pdf' => [
        'application/pdf',
        'application/x-pdf'
      ],
    ];

输出:字符串(150)“application / msword,application / vnd.ms-office,application / vnd.openxmlformats-officedocument.wordprocessingml.document,application / pdf,application / x-pdf”

文件上传后,我有任何类型的MIME规则失败。

所以,我做了这个测试:

$input = [
  'document' => $document // UploadedFile instance
];

$clientMime = $document->getClientMimeType() // output @example  application/pdf

$rules = [
  'document' => 'mimes:' . $clientMime;
];

$validator = Validator::make( $input, $rules );

$validator->fails()->errors() // mime fail

其他测试

上传.jpg文件(例如)MIME规则的作品

$rules = [
  'document' => 'mimes:jpeg,jpg';
];

$validator = Validator::make( $input, $rules ); // all right

有人可以帮忙吗?


从文档中,您可以像这样使用它:

'document' => 'mimes:pdf,doc'

试试这个,它应该工作:

$clientMime = $document->getClientMimeType() // output @example  application/pdf

$rules = [
  'document' => 'mimes:pdf';
];

$validator = Validator::make( $input, $rules );
链接地址: http://www.djcxy.com/p/45529.html

上一篇: Laravel validator mimes rule not working for documents

下一篇: PHP file upload on server changed mime