php Laravel 最大上传大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46067336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel max upload size limitations
提问by baig772
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?
我的网站托管在共享服务器上。最大上传限制为 5MB。如果上传的文件小于 5MB,则所有验证和文件上传都可以正常工作。但是当上传大于 5MB 的文件时,我看到了这个
。如何验证或强制文件低于服务器的上传限制?
采纳答案by patricus
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.
您似乎对更改 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.
您可以FileException
在您的异常处理程序中处理异常app/Exceptions/Handler.php
。更新render
方法以添加您需要的代码。例如,如果您想返回一个验证异常,您将需要在异常处理程序中处理该FileException
异常的验证。
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.
这是未经测试的,但应该给你一般的想法。
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.
您还可以通过 javascript 进行客户端验证,这样过大的文件实际上永远不会发送到您的服务器,但是客户端可以禁用或删除 javascript,因此最好设置好服务器端处理.
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.)
对于客户端验证,如果您将事件处理程序附加到文件输入的“更改”事件,您可以使用 来检查文件大小this.files[0].size
,然后执行任何其他操作(禁用表单、删除上传的文件等)
回答by AddWeb Solution Pvt Ltd
post_max_sizeand upload_max_filesizeare directives you can set to configure your php.ini file OR .htaccess file OR httpd.conf file.
post_max_size和upload_max_filesize是您可以设置以配置 php.ini 文件或 .htaccess 文件或 httpd.conf 文件的指令。
php.ini example:
php.ini 示例:
post_max_size=15M
upload_max_filesize=15M
.htaccess example:
.htaccess 示例:
php_value post_max_size=15M
php_value upload_max_filesize=15M
httpd.conf example:
httpd.conf 示例:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/example/
ErrorLog /var/log/httpd/error.log
<Directory /var/www/html/example/>
AllowOverride None
<IfModule mod_php5.c>
php_value post_max_size=15M
php_value upload_max_filesize=15M
</IfModule>
</Directory>
</VirtualHost>
回答by Exprator
$validator = Validator::make($request->all(), [
'file' => 'max:5120', //5MB
]);
in your controller
在你的控制器中
and in php.ini
并在 php.ini
upload_max_filesize = 10MB
回答by vivek s vamja
Change below Parameters in php.ini it works! for me.
在 php.ini 中更改下面的参数就可以了!为了我。
upload_max_filesize = 50M
memory_limit = 512M
max_input_time = -1
max_execution_time = 0
回答by Alankar More
You can use Laravel validation rules:
您可以使用 Laravel 验证规则:
'image_file_input_name' => 'required|mimes:jpeg,bmp,png|size:5000',
size:value
The field under validation must have a size matching the given value. For string data, the value corresponds to the number of characters. For numeric data, the value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.
大小:值
验证字段的大小必须与给定值匹配。对于字符串数据,该值对应于字符数。对于数值数据,该值对应于给定的整数值。对于数组,大小对应于数组的计数。对于文件,大小对应于以千字节为单位的文件大小。
回答by Quezler
The 5 mb limitation is set by your webhost and likely cannot be changed.
5 mb 限制由您的虚拟主机设置,可能无法更改。
To upload files larger than the limit you will have to upload it in parts.
要上传大于限制的文件,您必须分部分上传。
A laravel package that does so: https://github.com/jildertmiedema/laravel-plupload
这样做的 laravel 包:https: //github.com/jildertmiedema/laravel-plupload
回答by Farshad
you can check size of your file by maxvalidator
您可以通过最大验证器检查文件的大小
'file' => 'required|max:5128'; // max file size: 5M
回答by Suniti Yadav
In laravel you can validate like this -
在 Laravel 中,您可以像这样进行验证 -
$validator = Validator::make($request->all(), [
'file' => 'max:5120',
]);
The value is in kilobytes. i.e. max:10240 = max 10 MB. And in jquery -
该值以千字节为单位。即最大:10240 = 最大 10 MB。在 jquery 中 -
binds to onchange event of your input field
绑定到输入字段的 onchange 事件
$('#upFile').bind('change', function() {
//this.files[0].size gets the size of your file and then you can validate accourdingly...
alert(this.files[0].size);
});
Hope this will help you.
希望这会帮助你。
回答by Classified
You could do this with jQuery / javascript (Which means that the file will not be uploaded but you can check the file before it is sent to your PHP script)
您可以使用 jQuery / javascript 执行此操作(这意味着不会上传文件,但您可以在将文件发送到 PHP 脚本之前对其进行检查)
$('#myFile').bind('change', function() {
let filesize = this.files[0].size // On older browsers this can return NULL.
let filesizeMB = (filesize / (1024*1024)).toFixed(2);
if(filesizeMB <= 5) {
// Allow the form to be submitted here.
} else {
// Don't allow submission of the form here.
}
});
But always do serverside validation anyways. Incase you move to a stronger website and still want your website to work without any quirks.
但无论如何都要进行服务器端验证。如果您转移到一个更强大的网站,但仍然希望您的网站没有任何怪癖。
$validator = Validator::make($request->all(), [
'file' => 'max:5120', //5MB
]);
All of this is untested, but should work.
所有这些都未经测试,但应该有效。