Laravel验证器mimes规则不适用于文档

时间:2015-09-03 22:02:54

标签: php 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)&#34; application / msword,application / vnd.ms-office,application / vnd.openxmlformats-officedocument.wordprocessingml.document,application / pdf,application / x-pdf&#34; < / p>

文件上传后,我对任何类型都有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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

From the documentation,您可以这样使用它:

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

试试这个,它应该有效:

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

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

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