您似乎对更改PHP限制以允许更大的文件不感兴趣.在我看来,你希望你的最大上传量为5MB,如果超过这个上限,你会返回一个正确的回复.
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,所以最好设置好服务器端处理.
对于客户端验证,如果将事件处理程序附加到文件输入的"Change"事件,则可以使用this.files[0].size
判断文件大小,然后执行任何其他操作(禁用表单、删除上传的文件等).