PostTooLargeException in ValidatePostSize.php line 22 laravel

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

PostTooLargeException in ValidatePostSize.php line 22 laravel

phplaravellaravel-5.3

提问by Alex

I am trying to upload a form containing images too. When I submit it. it shows this error

我也在尝试上传包含图像的表单。当我提交时。它显示了这个错误

PostTooLargeException in ValidatePostSize.php line 22:

PostTooLargeException in ValidatePostSize.php line 22:

How do I resolve this issue?

我该如何解决这个问题?

回答by Dom DaFonte

Check the following parameters in your php.ini file.

检查 php.ini 文件中的以下参数。

I've had this issue on several occasions and it's usually because the max_file_size is set to 2M by default.

我曾多次遇到此问题,通常是因为默认情况下 max_file_size 设置为 2M。

  • max_file_size
  • upload_max_filesize
  • post_max_size
  • 最大文件大小
  • 上传最大文件大小
  • post_max_size

**Edit I was asked how to validate file size using the validate method in Laravel before the file is sent to PHP and alerting the user of the large file. You can and this is how:

**编辑我被问到如何在文件发送到 PHP 之前使用 Laravel 中的验证方法验证文件大小并提醒用户注意大文件。你可以,这是如何:

1. Create an error alert for the screenThis is what mine looks like. I use bootstrap 3 style. Add something like this into your layout (it will only appear if you have an error).

1. 为屏幕创建错误警报这是我的样子。我使用引导程序 3 样式。将类似的内容添加到您的布局中(只有在出现错误时才会出现)。

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br> 
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
        </div>
@endif

2. Identify the validator you will usewithin the pre-canned validation classes ** Go to your project/Resources/lang/en/validators.php You'll find all the validations available in laravel. You'll see they have this on in there:

2. 确定您将在预装验证类中使用的验证器** 转到您的项目/Resources/lang/en/validators.php 您将找到 laravel 中可用的所有验证。你会看到他们在那里有这个:

'max'                  => [
    'numeric' => 'The :attribute may not be greater than :max.',
    'file'    => 'The :attribute may not be greater than :max kilobytes.',
    'string'  => 'The :attribute may not be greater than :max characters.',
    'array'   => 'The :attribute may not have more than :max items.',
],

This is the validation rule I used to check file size.

这是我用来检查文件大小的验证规则。

**4. Create your request file **

**4. 创建您的请求文件 **

php artisan make:request yourRequest

**5. update your request file ** Go to yourProject/app/Http/Requests/yourRequest.php and add the following in the rules method 'file_name' => 'max:10' update 10 to the value of your limit in kilobytes:

**5. 更新您的请求文件 ** 转到 yourProject/app/Http/Requests/yourRequest.php 并在规则方法中添加以下内容 'file_name' => 'max:10' 将 10 更新为以千字节为单位的限制值:

    public function rules()
    {
        return [ 'profile_pic' => 'required|image|mimes:jpg|max:10',
        ];
    }

6. Make your Request with whichever the request file is managing your rules.So in this scenario we named it yourRequest, so your save method would have:

6. 使用管理规则的请求文件提出请求。因此,在这种情况下,我们将其命名为 yourRequest,因此您的保存方法将具有:

public function upload(Requests\yourRequest $request)

Also make sure your Controller uses the requests class in it like so:

还要确保您的控制器使用其中的请求类,如下所示:

use App\Http\Requests;

If you follow this you'll have an error that looks like this: enter image description here

如果您按照此操作,您将收到如下所示的错误: 在此处输入图片说明

The other option is to shrink the file (in the case of an image) using the Image class.

另一种选择是使用 Image 类缩小文件(在图像的情况下)。

回答by Naveen dev

You should use client-side validation on upload file and if there are multiple files to upload options you should insert data through of javascript or one by one

您应该对上传文件使用客户端验证,如果有多个文件要上传选项,您应该通过 javascript 或一一插入数据

Example code:

示例代码:

 $(document).on("change", "#imageUploader", function(e) {
      if(this.files[0].size > 7244183)
      {
        alert("The file size is too larage"); 
        document.getElementById('#imageUploader').value = "";
      }
});