• <i id='UQ7DI'><tr id='UQ7DI'><dt id='UQ7DI'><q id='UQ7DI'><span id='UQ7DI'><b id='UQ7DI'><form id='UQ7DI'><ins id='UQ7DI'></ins><ul id='UQ7DI'></ul><sub id='UQ7DI'></sub></form><legend id='UQ7DI'></legend><bdo id='UQ7DI'><pre id='UQ7DI'><center id='UQ7DI'></center></pre></bdo></b><th id='UQ7DI'></th></span></q></dt></tr></i><div id='UQ7DI'><tfoot id='UQ7DI'></tfoot><dl id='UQ7DI'><fieldset id='UQ7DI'></fieldset></dl></div>
      <bdo id='UQ7DI'></bdo><ul id='UQ7DI'></ul>

  • <small id='UQ7DI'></small><noframes id='UQ7DI'>

    <tfoot id='UQ7DI'></tfoot><legend id='UQ7DI'><style id='UQ7DI'><dir id='UQ7DI'><q id='UQ7DI'></q></dir></style></legend>

      1. Laravel - 超过 PHP 最大上传大小限制时验证文件大

        时间:2023-05-21
          <bdo id='y5nkw'></bdo><ul id='y5nkw'></ul>
            <tfoot id='y5nkw'></tfoot>

                <i id='y5nkw'><tr id='y5nkw'><dt id='y5nkw'><q id='y5nkw'><span id='y5nkw'><b id='y5nkw'><form id='y5nkw'><ins id='y5nkw'></ins><ul id='y5nkw'></ul><sub id='y5nkw'></sub></form><legend id='y5nkw'></legend><bdo id='y5nkw'><pre id='y5nkw'><center id='y5nkw'></center></pre></bdo></b><th id='y5nkw'></th></span></q></dt></tr></i><div id='y5nkw'><tfoot id='y5nkw'></tfoot><dl id='y5nkw'><fieldset id='y5nkw'></fieldset></dl></div>
                <legend id='y5nkw'><style id='y5nkw'><dir id='y5nkw'><q id='y5nkw'></q></dir></style></legend>
                  <tbody id='y5nkw'></tbody>

                <small id='y5nkw'></small><noframes id='y5nkw'>

                  本文介绍了Laravel - 超过 PHP 最大上传大小限制时验证文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我的网站托管在共享服务器上.最大上传限制为 5MB.我无法更改 PHP ini 设置,这很好.

                  I have my website hosted at a shared server. The maximum upload limit is 5MB. I do not have the ability to change the PHP ini settings, which is fine.

                  在 Laravel 中,如果上传的文件小于 5MB,所有验证和文件上传都可以正常工作.但是,当上传大于 5MB 的文件时,我收到以下异常而不是验证错误:

                  In Laravel, all the validations and file uploading works fine if the uploaded file is under 5MB. However, when a file greater than 5MB is uploaded, I get the following exception instead of a validation error:

                  如何验证或强制文件低于服务器上传限制?

                  How can I validate or force the file to be under the upload limit from server?

                  推荐答案

                  您似乎对更改 PHP 限制以允许更大的文件不感兴趣.在我看来,您希望最大上传大小为 5MB,如果超过,则返回正确的响应.

                  You don't seem interested in changing the PHP limits to allow larger files. It looks to me like you want your max upload to be 5MB, and return a proper response if it is over that.

                  您可以在 app/Exceptions/Handler.php 处的异常处理程序中处理 FileException 异常.更新 render 方法以添加您需要的代码.例如,如果您想返回验证异常,则需要在 FileException 异常的异常处理程序中处理验证.

                  You can handle the FileException exception inside your exception handler at app/Exceptions/Handler.php. Update the render method to add in the code you need. For example, if you'd like to return a validation exception, you will need to handle the validation inside the exception handler for the FileException exception.

                  public function render($request, Exception $exception)
                  {
                      if ($exception instanceof SymfonyComponentHttpFoundationFileExceptionFileException) {
                          // create a validator and validate to throw a new ValidationException
                          return Validator::make($request->all(), [
                              'your_file_input' => 'required|file|size:5000',
                          ])->validate();
                      }
                  
                      return parent::render($request, $exception);
                  }
                  

                  这是未经测试的,但应该可以让您大致了解.

                  This is untested, but should give you the general idea.

                  您还可以通过 javascript 进行客户端验证,这样过大​​的文件实际上永远不会发送到您的服务器,但是客户端可以禁用或删除 javascript,因此最好有一个不错的服务器端处理设置.

                  You can also do client side validation via javascript, so that a file that is too large is never actually sent to your server, but javascript can be disabled or removed by the client, so it would be good to have nice server side handling set up.

                  对于客户端验证,如果您将事件处理程序附加到文件输入的更改"事件,您可以使用 this.files[0].size 检查文件大小,并且之后执行任何其他操作(禁用表单、删除上传的文件等)

                  For the client side validation, if you attach an event handler to the "change" event for the file input, you can check the file size using this.files[0].size, and perform any additional actions after that (disable form, remove uploaded file, etc.)

                  这篇关于Laravel - 超过 PHP 最大上传大小限制时验证文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何通过 Jquery/AJAX 上传文件 下一篇:通过ajax post将文件与表单数据一起发送

                  相关文章

                  最新文章

                    <bdo id='HpDrZ'></bdo><ul id='HpDrZ'></ul>
                  <legend id='HpDrZ'><style id='HpDrZ'><dir id='HpDrZ'><q id='HpDrZ'></q></dir></style></legend>
                  <i id='HpDrZ'><tr id='HpDrZ'><dt id='HpDrZ'><q id='HpDrZ'><span id='HpDrZ'><b id='HpDrZ'><form id='HpDrZ'><ins id='HpDrZ'></ins><ul id='HpDrZ'></ul><sub id='HpDrZ'></sub></form><legend id='HpDrZ'></legend><bdo id='HpDrZ'><pre id='HpDrZ'><center id='HpDrZ'></center></pre></bdo></b><th id='HpDrZ'></th></span></q></dt></tr></i><div id='HpDrZ'><tfoot id='HpDrZ'></tfoot><dl id='HpDrZ'><fieldset id='HpDrZ'></fieldset></dl></div>

                  1. <tfoot id='HpDrZ'></tfoot>

                  2. <small id='HpDrZ'></small><noframes id='HpDrZ'>