Laravel 验证 pdf mime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35694905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:17:31  来源:igfitidea点击:

Laravel validation pdf mime

validationlaravelmime

提问by amin

I have tried below mime types for validating PDF files.but none of them doesnt pass the validation .

我已经尝试过以下 mime 类型来验证 PDF 文件。但它们都没有通过验证。

  $rules  = [
     ....
    "file" => "required|mimes:application/pdf, application/x-pdf,application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf|max:10000"
     ....
       ]

回答by Froxz

Just add file extension

只需添加文件扩展名

$rules  = [
    "file" => "required|mimes:pdf|max:10000"
]

From laravel Docs:

来自 Laravel 文档:

Even though you only need to specify the extensions, this rule actually validates against the MIME type of the file by reading the file's contents and guessing its MIME type.

即使您只需要指定扩展名,该规则实际上通过读取文件内容并猜测其MIME 类型来验证文件的MIME 类型



Update:

更新:

Starting from Laravel 5.2, you could validate file upload against full MIME type, new validationrule called: mimetypes.

从开始Laravel 5.2,你可以验证对全MIME类型,文件上传新的验证规则叫做:mimetypes

$rules  = [
    "file" => "required|mimetypes:application/pdf|max:10000"
]