php 如何验证 Laravel 中的最大文件大小?

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

How to Validate on Max File Size in Laravel?

phpvalidationlaravel

提问by Jim Peeters

I'm trying to validate on a max file size of 500kb in Laravel:

我正在尝试在 Laravel 中验证最大 500kb 的文件大小:

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

But this says that the file should be exactly 500kb big. How can I edit this rule so that it returns an error when it's bigger than 500kb?

但这表示该文件应该正好是 500kb 大。如何编辑此规则,使其在大于 500kb 时返回错误?

Ive tried this:

我试过这个:

'file' => 'size:>=500'
'file'  => 'size:max:500'

The documentation says nothing about this:

文档没有说明这一点:

size:value

The field under validation must have a size matching the given value. For string data, the value corresponds to the number of characters. For numeric data, the value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.

大小:值

验证字段的大小必须与给定值匹配。对于字符串数据,该值对应于字符数。对于数值数据,该值对应于给定的整数值。对于文件,大小对应于以千字节为单位的文件大小。

回答by Filip Koblański

According to the documentation:

根据文档:

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

The value is in kilobytes. I.e. max:10240= max 10 MB.

该值以千字节为单位。即max:10240= 最大 10 MB。

回答by NULL pointer

Edit: Warning! This answer worked on my XAMPP OsX environment, but when I deployed it to AWS EC2 it did NOT prevent the upload attempt.

编辑:警告!这个答案适用于我的 XAMPP OsX 环境,但是当我将它部署到 AWS EC2 时,它并没有阻止上传尝试。

I was tempted to delete this answer as it is WRONG But instead I will explain what tripped me up

我很想删除这个答案,因为它是错误的但相反,我会解释是什么让我绊倒了

My file upload field is named 'upload' so I was getting "The upload failed to upload.". This message comes from this line in validation.php:

我的文件上传字段被命名为“上传”,所以我收到“上传失败上传。”。此消息来自validation.php 中的这一行:

in resources/lang/en/validaton.php:

在 resources/lang/en/validaton.php 中:

'uploaded' => 'The :attribute failed to upload.',

'uploaded' => 'The :attribute failed to upload.',

And this is the message displayed when the file is larger than the limit set by PHP.

这是文件大于 PHP 设置的限制时显示的消息。

I want to over-ride this message, which you normally can do by passing a third parameter $messages array to Validator::make() method.

我想覆盖此消息,您通常可以通过将第三个参数 $messages 数组传递给 Validator::make() 方法来实现。

HoweverI can't do that as I am calling the POST from a React Component, which renders the form containing the csrf field and the upload field.

但是我不能这样做,因为我从 React 组件调用 POST,它呈现包含 csrf 字段和上传字段的表单。

So instead, as a super-dodgy-hack, I chose to get into my view that displays the messages and replace that specific message with my friendly 'file too large' message.

因此,作为一个超级狡猾的黑客,我选择进入显示消息的视图,并将该特定消息替换为我友好的“文件太大”消息。

Here is what works if the file to smaller than the PHP file size limit:

如果文件小于 PHP 文件大小限制,以下是有效的方法:

In case anyone else is using Laravel FormRequest class, here is what worked for me on Laravel 5.7:

如果其他人正在使用 Laravel FormRequest 类,以下是在 Laravel 5.7 上对我有用的内容:

This is how I set a custom error message and maximum file size:

这是我设置自定义错误消息和最大文件大小的方式:

I have an input field <input type="file" name="upload">. Note the CSRF token is required also in the form (google laravel csrf_field for what this means).

我有一个输入字段<input type="file" name="upload">。请注意,表单中也需要 CSRF 令牌(google laravel csrf_field 这意味着什么)。

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Upload extends FormRequest
{
  ...
  ...
  public function rules() {
    return [
      'upload' => 'required|file|max:8192',
    ];
  }
  public function messages()
  {
    return [            
      'upload.required' => "You must use the 'Choose file' button to select which file you wish to upload",
      'upload.max' => "Maximum file size to upload is 8MB (8192 KB). If you are uploading a photo, try to reduce its resolution to make it under 8MB"
    ];
  }
}