Browse Source

Fix error where multiple files at would be handled incorrectly.

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.
mark_story 11 years ago
parent
commit
57c9ba3f7d
2 changed files with 27 additions and 10 deletions
  1. 5 4
      src/Network/Request.php
  2. 22 6
      tests/TestCase/Network/RequestTest.php

+ 5 - 4
src/Network/Request.php

@@ -427,17 +427,18 @@ class Request implements \ArrayAccess {
 	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;

+ 22 - 6
tests/TestCase/Network/RequestTest.php

@@ -281,25 +281,31 @@ class RequestTest extends TestCase {
 			],
 			'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'
@@ -326,6 +332,16 @@ class RequestTest extends TestCase {
 						'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 => [