ValidatePostSize.php第22行中的PostTooLargeException [英] PostTooLargeException in ValidatePostSize.php line 22 laravel

查看:98
本文介绍了ValidatePostSize.php第22行中的PostTooLargeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传也包含图像的表格.当我提交时.它显示此错误

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

PostTooLargeException in ValidatePostSize.php line 22:

如何解决此问题?

推荐答案

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

Check the following parameters in your php.ini file.

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

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
  • upload_max_filesize
  • post_max_size

**编辑 在将文件发送到PHP并警告用户大文件之前,有人问我如何使用Laravel中的validate方法验证文件大小.您可以,而且方法如下:

**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:

1.为屏幕创建错误警报 这就是我的样子.我使用bootstrap 3样式.在布局中添加类似的内容(只有在出现错误时才会显示).

1. Create an error alert for the screen This 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).

    @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.在预设的验证类别中确定要使用的验证器 ** 转到您的project/Resources/lang/zh-CN/validators.php,您将在laravel中找到所有可用的验证.您会看到他们那里有这样的东西:

2. Identify the validator you will use within 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:

'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.创建您的请求文件**

**4. Create your request file **

php artisan make:request yourRequest

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

**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:

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

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

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:

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:

另一个选择是使用Image类缩小文件(对于图像).

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

这篇关于ValidatePostSize.php第22行中的PostTooLargeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆