Laravel 验证允许图像或 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39467354/
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-14 14:27:09  来源:igfitidea点击:

Laravel validation allow image or pdf

phplaravellaravel-5

提问by Paul Lucero

I have a file input in Laravel and I would like it to only allow images or pdf. How do I do this in controller validation?

我在 Laravel 中有一个文件输入,我希望它只允许图像或 pdf。如何在控制器验证中执行此操作?

$validator = Validator::make($request->all(), [
    'image' => 'image',
]);

回答by Hammerbot

You should rather make a MIME type validation:

您应该进行 MIME 类型验证

$validator = Validator::make($request->all(), [
    'image' => 'mimes:jpeg,bmp,png,gif,svg,pdf',
]);

回答by ako

You may validate like below for different mime types:

您可以针对不同的 mime 类型进行如下验证:

    $allowed_mimes = [
         image/gif,
         image/png,
         image/jpeg,
         image/bmp,
         image/webp,
         application/octet-stream, application/pkcs12,
         application/vnd.mspowerpoint,
         application/xhtml+xml,
         application/xml,
         application/pdf
    ];

$validator = Validator::make($request->all(), [
    'image' => $allowed_mimes,
]);

See this.

看到这个