加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在ValidatePostSize.php第22行laravel中的PostTooLargeException

发布时间:2020-12-14 19:50:55 所属栏目:大数据 来源:网络整理
导读:我正在尝试上传包含图片的表单.当我提交它.它显示了这个错误 ValidatePostSize.php第22行中的PostTooLargeException: 我该如何解决这个问题? 检查php.ini文件中的以下参数. 我曾多次遇到过这个问题,通常是因为max_file_size默认设置为2M. max_file_size up
我正在尝试上传包含图片的表单.当我提交它.它显示了这个错误

ValidatePostSize.php第22行中的PostTooLargeException:

我该如何解决这个问题?

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

我曾多次遇到过这个问题,通常是因为max_file_size默认设置为2M.

> max_file_size
> upload_max_filesize
> post_max_size

**编辑
我被问到如何在将文件发送到PHP之前使用Laravel中的validate方法验证文件大小,并向用户警告大文件.你可以这样做:

1.为屏幕创建错误警报
这就是我的样子.我使用bootstrap 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.确定您将在预先确定的验证类中使用的验证器**
转到您的项目/ 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.',],

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

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

php artisan make:request yourRequest

** 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.使用请求文件管理规则的任何一方提出请求.
因此,在这种情况下,我们将其命名为yourRequest,因此您的save方法将具有:

public function upload(RequestsyourRequest $request)

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

use AppHttpRequests;

如果你这样做,你会有一个如下错误:

另一种选择是使用Image类缩小文件(如果是图像).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读