Skip to content

Commit

Permalink
Fix error where multiple files at would be handled incorrectly.
Browse files Browse the repository at this point in the history
When there are multiple files at the same level the path needs to be
reset on each iteration. Failing to do so causes adjacent files to be
nested incorrectly.
  • Loading branch information
markstory committed Jul 19, 2014
1 parent 8a2805f commit 57c9ba3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/Network/Request.php
Expand Up @@ -427,17 +427,18 @@ protected function _processFiles($post, $files) {
protected function _processFileData($data, $post, $path = '', $field = '') {
foreach ($post as $key => $fields) {
$newField = $field;
$newPath = $path;
if ($path === '' && $newField === '') {
$newField = $key;
}
if ($field === $newField) {
$path .= '.' . $key;
$newPath .= '.' . $key;
}
if (is_array($fields)) {
$data = $this->_processFileData($data, $fields, $path, $newField);
$data = $this->_processFileData($data, $fields, $newPath, $newField);
} else {
$path = trim($path . '.' . $field, '.');
$data = Hash::insert($data, $path, $fields);
$newPath = trim($newPath . '.' . $field, '.');
$data = Hash::insert($data, $newPath, $fields);
}
}
return $data;
Expand Down
28 changes: 22 additions & 6 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -281,25 +281,31 @@ public function testProcessFilesNested() {
],
'pictures' => [
'name' => [
0 => ['file' => 'a-file.png']
0 => ['file' => 'a-file.png'],
1 => ['file' => 'a-moose.png']
],
'type' => [
0 => ['file' => 'image/png']
0 => ['file' => 'image/png'],
1 => ['file' => 'image/jpg']
],
'tmp_name' => [
0 => ['file' => '/tmp/file123']
0 => ['file' => '/tmp/file123'],
1 => ['file' => '/tmp/file234']
],
'error' => [
0 => ['file' => '0']
0 => ['file' => '0'],
1 => ['file' => '0']
],
'size' => [
0 => ['file' => 17188]
0 => ['file' => 17188],
1 => ['file' => 2010]
],
]
];
$post = [
'pictures' => [
0 => ['name' => 'A cat']
0 => ['name' => 'A cat'],
1 => ['name' => 'A moose']
],
0 => [
'name' => 'A dog'
Expand All @@ -326,6 +332,16 @@ public function testProcessFilesNested() {
'error' => '0',
'size' => 17188,
]
],
1 => [
'name' => 'A moose',
'file' => [
'name' => 'a-moose.png',
'type' => 'image/jpg',
'tmp_name' => '/tmp/file234',
'error' => '0',
'size' => 2010,
]
]
],
0 => [
Expand Down

0 comments on commit 57c9ba3

Please sign in to comment.