framework: [5.1] [5.2] Required file rules dont work when file size greater than max_file_upload size
This issue has come up before: https://github.com/laravel/framework/issues/4467 & https://github.com/laravel/framework/issues/4226
@GrahamCampbell said in #4467 that there is an existing pull that deals with this - but it is not fixed - it is still broken - and I cant find any PR that seems to deal with this?
The problem is simple:
- Upload file greater than
max_file_upload(but less thanpost_max_size) - PHP will send the request to Laravel, but has cleared the
filebecause it is larger thanmax_file_upload - Laravel does a
requiredcheck - sees the file is not there, and reports “The file is required”.
Obvious this is very confusing to users - because they did attach a file - but the website is telling them they didnt.
The general solution would be to add a simple check to see if there is an error on the $_FILES global - which would indicate there was a file attached. But then you run into a new problem - if the file is required, but it is not actually there - then you cant let required pass - it must fail.
So I think the solution is to return a custom message for file validations - with some checks as to why the required failed?
I’m happy to do the PR myself - but I’m looking for some guidance/direction on how we can solve this problem…
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 26 (12 by maintainers)
I’ve done a picture to try to illustrate the problem
Although I may not hold much weight as I am new here, I would agree with @Arrilot that it would be better to modify the existing “required” rule than to force users to add a new “file” rule to any file upload validations.
A bugfix should not require end users to take an action, it should be invisible to them. Although adding a “file” validation rule is something that can be discussed as a new feature, it’s separate from the bug immediately at hand.
{!! Form::open(array('route' => 'resizeImagePost', 'files' => true)) !!}I’ve been digging around the validation rules - and I have an idea how we might solve this, plus improve the validation rules at the same time.
The validation rule
requiredfor files is actually a bit strange:The only way a
$valuecan be an instanceOf File is if the user originally sent a file. So by definition - this means the required rule has been meet, and should always pass.The problem is that if the file upload had an error, then the
->getPath()returns blank, and the required rule is nowfalse- giving an incorrect error “File is required” - when it should be more like “There was an error uploading”.I propose we remove that
elseifstatement from therequiredrules.And we add a
filevalidation rule. We havestringandarrayvalidation rules. We also have animagerule. But we dont have a generalfilevalidation rule? If you want to allow any file, it is difficult without settingmimes- but what if you want to allow anymime?The
filerule could be something like this (needs refactoring - but you get the idea):Then in your validation rules you just have to do
or you can do
Which defaults to min size of
1(because you shouldnt accept filesizes of0bytes), and max file size of whatever your PHPmax_file_uploadconfig is (which is your limit no matter what).and you can easily combine it with
imageandmimerulesor like this:
@GrahamCampbell - no - separate issue.
#8202 is when post >
post_max_sizeThis is when file >
max_file_upload- which is much more common