Laravel最大上传大小限制 [英] Laravel max upload size limitations

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

问题描述

我的网站托管在共享服务器上.最高上传限制为5MB.如果上传的文件小于5MB,则所有验证和文件上传均可以正常工作.但是当上传大于5MB的文件时,我看到了 . 如何验证或强制文件低于服务器的上传限制?

I have my website hosted at a shared server. The maximum upload limit is 5MB. All the validations and file uploading works fine if the uploaded file is under 5MB. But when a file greater than 5MB is uploaded, i see this . 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 \Symfony\Component\HttpFoundation\File\Exception\FileException) {
        // 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.

对于客户端验证,如果将事件处理程序附加到文件输入的"change"事件,则可以使用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最大上传大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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