douzuanze0486 2017-02-02 11:45
浏览 308
已采纳

在ValidatePostSize.php第22行laravel中的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:

How do I resolve this issue?

  • 写回答

2条回答 默认 最新

  • drs3925 2017-02-02 18:04
    关注

    Check the following parameters in your php.ini file.

    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

    **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. 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. 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. Create your request file **

    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:

        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:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?