2017-08-08 77 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'];