在ValidatePostSize.php第22行laravel中的PostTooLargeException

Ale*_*lex 6 php laravel laravel-5.3

我正在尝试上传包含图片的表单.当我提交它.它显示了这个错误

PostTooLargeException in ValidatePostSize.php line 22:

我该如何解决这个问题?

Dom*_*Dom 11

检查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
Run Code Online (Sandbox Code Playgroud)

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.',
],
Run Code Online (Sandbox Code Playgroud)

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

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

php artisan make:request yourRequest
Run Code Online (Sandbox Code Playgroud)

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

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

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

public function upload(Requests\yourRequest $request)
Run Code Online (Sandbox Code Playgroud)

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

use App\Http\Requests;
Run Code Online (Sandbox Code Playgroud)

如果你这样做,你会有一个如下错误: 在此输入图像描述

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

  • 此外,[memory_limit](http://php.net/manual/en/ini.core.php#ini.memory-limit)也发挥了作用.如果post_max_size的值大于memory_limit,则必须增加memory_limit以使其大于post_max_size. (2认同)