Laravel Mimetypes验证始终为假

时间:2017-08-08 13:52:52

标签: laravel validation mime-types

我正在上传文件,想要检查它是否符合以下条件:

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."

mimetype猜测似乎是正确的。例如,我尝试上传foobar.csv

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

但是,它仍然会返回The file must be a file of type [...]

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

尝试以下代码,按RFC 2616 7.2.1添加'application / octet-stream'。

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'];
}

答案 1 :(得分: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'];