2017-08-08 68 views
0

我上传文件,并要检查它匹配下面:Laravel MIMETYPES验证始终为假

public function rules() 
    { 
     return ['file' => 'required|mimetypes: 
            application/vnd.ms-excel, 
            application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, 
            application/pdf, 
            text/plain, 
            text/csv, 
            csv, 
            txt, 
            text/tsv, 
            image/jpeg, 
            image/png, 
            image/svg+xml, 
            image/tiff, 
            video/x-msvideo, 
            video/mpeg 
            |max:12345678']; 
} 

然而,总是回报“The file must be a file of type: application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/pdf, text/plain, text/csv, csv, txt, text/tsv, image/jpeg, image/png, image/svg+xml, image/tiff, video/x-msvideo, video/mpeg.

的MIME类型的猜测似乎是正确的。举例来说,我试着上传foobar.csv

$request->file->getMimeType() // text/plain 

不过不失,但仍返回The file must be a file of type [...]

我在这里错过了什么?

回答

0

尝试下面的代码,添加了'application/octet-stream'按照RFC 2616 7.2.1

public function rules() 
    { 
     return ['file' => 'required|mimetypes: 
            application/vnd.ms-excel, 
            application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, 
            application/pdf, 
            text/plain, 
            text/csv, 
            application/octet-stream, 
            csv, 
            txt, 
            text/tsv, 
            image/jpeg, 
            image/png, 
            image/svg+xml, 
            image/tiff, 
            video/x-msvideo, 
            video/mpeg 
            |max:12345678']; 
} 
+0

我尝试添加,但结果是一样的 – Wistar

0

问题是mimetypes被指定为一个字符串。在字符串中,我添加了代码清晰的新行,这被解释为这样。当我删除他们有一个样样在行,它的工作:

return ['file' => 'required|mimetypes:application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf,application/octet-stream,text/plain,text/csv,csv,txt,text/tsv,image/jpeg,image/png,image/svg+xml,image/tiff,video/x-msvideo,video/mpeg|max:12345678'];