Skip to content

Commit

Permalink
bug #24198 [HttpFoundation] Fix file upload multiple with no files (e…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fabpot committed Sep 30, 2017
2 parents 72cc5df + d4f6039 commit 166f64e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/FileBag.php
Expand Up @@ -67,7 +67,7 @@ public function add(array $files = array())
*
* @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
*
* @return UploadedFile|UploadedFile[] A (multi-dimensional) array of UploadedFile instances
* @return UploadedFile[]|UploadedFile|null A (multi-dimensional) array of UploadedFile instances
*/
protected function convertFileInformation($file)
{
Expand All @@ -87,7 +87,7 @@ protected function convertFileInformation($file)
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
}
} else {
$file = array_map(array($this, 'convertFileInformation'), $file);
$file = array_filter(array_map(array($this, 'convertFileInformation'), $file));
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php
Expand Up @@ -60,6 +60,19 @@ public function testShouldSetEmptyUploadedFilesToNull()
$this->assertNull($bag->get('file'));
}

public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
{
$bag = new FileBag(array('file' => array(
'name' => array(''),
'type' => array(''),
'tmp_name' => array(''),
'error' => array(UPLOAD_ERR_NO_FILE),
'size' => array(0),
)));

$this->assertSame(array(), $bag->get('file'));
}

public function testShouldConvertUploadedFilesWithPhpBug()
{
$tmpFile = $this->createTempFile();
Expand Down

0 comments on commit 166f64e

Please sign in to comment.