Skip to content

Commit

Permalink
Convert FILES processing to use recursion.
Browse files Browse the repository at this point in the history
Fixes issues with deeply nested file data issues.
Fixes cakephp#2796
  • Loading branch information
markstory committed Apr 18, 2012
1 parent c15259f commit b54c3b0
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/Cake/Network/CakeRequest.php
Expand Up @@ -320,21 +320,31 @@ protected function _processFiles() {

if (isset($_FILES['data'])) {
foreach ($_FILES['data'] as $key => $data) {
foreach ($data as $model => $fields) {
if (is_array($fields)) {
foreach ($fields as $field => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$this->data[$model][$field][$k][$key] = $v;
}
} else {
$this->data[$model][$field][$key] = $value;
}
}
} else {
$this->data[$model][$key] = $fields;
}
}
$this->_processFileData('', $data, $key);
}
}
}

/**
* Recursively walks the FILES array restructuring the data
* into something sane and useable.
*
* @param string $path The dot separated path to insert $data into.
* @param array $data The data to traverse/insert.
* @param string $field The terminal field name, which is the top level key in $_FILES.
* @return void
*/
protected function _processFileData($path, $data, $field) {
foreach ($data as $key => $fields) {
$newPath = $key;
if (!empty($path)) {
$newPath = $path . '.' . $key;
}
if (is_array($fields)) {
$this->_processFileData($newPath, $fields, $field);
} else {
$newPath .= '.' . $field;
$this->data = Set::insert($this->data, $newPath, $fields);
}
}
}
Expand Down

0 comments on commit b54c3b0

Please sign in to comment.