Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[HttpFoundation] Fix file upload multiple with no files#24198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
xabbuh commentedSep 14, 2017
The |
enumag commentedSep 14, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@xabbuh It does not work because the null is somehow converted to an empty string. I don't know when or why. And regardless I believe this should be fixed in HttpFoundation anyway - some people are using HttpFoundation without Forms. Should I revert the FileType fix as part of this PR? I don't think it's the right place to fix this issue. |
xabbuh commentedSep 14, 2017
Not sure about reverting the change inside the |
enumag commentedSep 14, 2017
Of course. I wouldn't want you to merge it without a test anyway. |
d111996 to748223cCompareenumag commentedSep 14, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
| publicfunctiontestShouldRemoveEmptyUploadedFilesForMultiUpload() | ||
| { | ||
| $bag =newFileBag(array('file' =>array( | ||
| 'name' =>array(''), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Is this really the structure that is used when multiple files are submitted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@xabbuh Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
To be more precise it's the structure used for<input type="file" name="file[]"> (note the[]), regardless of how many files are actually sent (including 0) and regardless of themultiple attribute.
You can try yourself with the example script I posted above. Just addvar_export($_FILES); somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
For the records, that's also explicitly described here:http://php.net/manual/en/features.file-upload.multiple.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I can't reproduce the issue, thisFileBag with those values returns:
according to:
symfony/src/Symfony/Component/HttpFoundation/FileBag.php
Lines 86 to 87 in12f1239
| if (UPLOAD_ERR_NO_FILE ==$file['error']) { | |
| $file =null; |
the rest is handled onPRE_SUBMIT event insideFileType ifmultiple = true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
See test case also:
symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php
Lines 99 to 110 in77653d1
| publicfunctiontestSubmitNullWhenMultiple() | |
| { | |
| $form =$this->factory->create(static::TESTED_TYPE,null,array( | |
| 'multiple' =>true, | |
| )); | |
| // submitted data when an input file is uploaded without choosing any file | |
| $form->submit(array(null)); | |
| $this->assertSame(array(),$form->getData()); | |
| $this->assertSame(array(),$form->getNormData()); | |
| $this->assertSame(array(),$form->getViewData()); | |
| } |
xabbuh left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
looks good to me
xabbuh commentedSep 14, 2017
@enumag Can you rebase and change the target branch to |
enumag commentedSep 14, 2017
@xabbuh Sure. Can I ask why? I mean according toSymfony Release Process maintenance for 2.7 already ended. I don't think this qualifies for a security fix. |
xabbuh commentedSep 14, 2017
Symfony is an LTS release it receives bugfix until May 2018. |
enumag commentedSep 14, 2017
Oh right, my mistake. I'll rebase it soon. |
enumag commentedSep 14, 2017
@xabbuh All done. |
yceruto commentedSep 14, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Yep, I think |
enumag commentedSep 18, 2017
@xabbuh Should I remove the buildForm here or in another PR? Perhaps it should only be removed in newer symfony versions to prevent regression when new version of Form component is used with old version of HttpFoundation? |
xabbuh commentedSep 20, 2017
Wecould remove the Form related part, but as you said it may hurt people mixing different versions of the HttpFoundation and Form components. Thus, as it doesn't hurt, I'd vote to keep it as is. |
enumag commentedSep 20, 2017
Alright. I will send a PR to Forms later when there is no possibility of such conflict. |
fabpot commentedSep 30, 2017
Thank you@enumag. |
…numag)This PR was merged into the 2.7 branch.Discussion----------[HttpFoundation] Fix file upload multiple with no files| Q | A| ------------- | ---| Branch? | 2.7| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets || License | MIT| Doc PR |```php<form method="post" enctype="multipart/form-data"><input type="file" multiple name="img[]"><input type="submit"></form><?php$loader = require __DIR__ . '/../app/autoload.php';if ($_SERVER['REQUEST_METHOD'] === 'POST') { $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); var_export($request->files->all()['img']);}```Expected result when I send the form without any files:```array ()```Actual result:```array ( 0 => NULL, )```This causes a problem later when using FileType with multiple option - if no files are sent the form data are `[0 => '']` instead of `[]`.Of course I need to add a test for this.Commits-------d4f6039 [HttpFoundation] Fix file upload multiple with no files
Uh oh!
There was an error while loading.Please reload this page.
Expected result when I send the form without any files:
Actual result:
This causes a problem later when using FileType with multiple option - if no files are sent the form data are
[0 => '']instead of[].Of course I need to add a test for this.