Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 166f64e

Browse filesBrowse files
committed
bug #24198 [HttpFoundation] Fix file upload multiple with no files (enumag)
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
2 parents 72cc5df + d4f6039 commit 166f64e
Copy full SHA for 166f64e

File tree

2 files changed

+15
-2
lines changed
Filter options

2 files changed

+15
-2
lines changed

‎src/Symfony/Component/HttpFoundation/FileBag.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/FileBag.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function add(array $files = array())
6767
*
6868
* @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
6969
*
70-
* @return UploadedFile|UploadedFile[] A (multi-dimensional) array of UploadedFile instances
70+
* @return UploadedFile[]|UploadedFile|null A (multi-dimensional) array of UploadedFile instances
7171
*/
7272
protected function convertFileInformation($file)
7373
{
@@ -87,7 +87,7 @@ protected function convertFileInformation($file)
8787
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
8888
}
8989
} else {
90-
$file = array_map(array($this, 'convertFileInformation'), $file);
90+
$file = array_filter(array_map(array($this, 'convertFileInformation'), $file));
9191
}
9292
}
9393

‎src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ public function testShouldSetEmptyUploadedFilesToNull()
6060
$this->assertNull($bag->get('file'));
6161
}
6262

63+
public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
64+
{
65+
$bag = new FileBag(array('file' => array(
66+
'name' => array(''),
67+
'type' => array(''),
68+
'tmp_name' => array(''),
69+
'error' => array(UPLOAD_ERR_NO_FILE),
70+
'size' => array(0),
71+
)));
72+
73+
$this->assertSame(array(), $bag->get('file'));
74+
}
75+
6376
public function testShouldConvertUploadedFilesWithPhpBug()
6477
{
6578
$tmpFile = $this->createTempFile();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.