Laravel验证允许图像或pdf

时间:2016-09-13 10:06:39

标签: php laravel laravel-5

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

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

2 个答案:

答案 0 :(得分:10)

你应该make a MIME type validation

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

答案 1 :(得分:-1)

您可以针对不同的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,
]);

请参阅this