Laravel validator mimes rule not working for documents

i'm trying to upload document files as pdf, doc, docx. To make i'm using this values:

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

output: string(150) "application/msword,application/vnd.ms-office,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,application/x-pdf"

After file upload, i had mime rule fail for any types.

So, i do this test:

$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

OTHER TEST

Uploading a .jpg file (for example) the mime rule works

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

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

Can someone help?


From the documentation, you use it like this:

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

Try this, and it should work:

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

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

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

上一篇: 使用PHP上传时出现MIME错误

下一篇: Laravel验证者哑剧规则不适用于文档