Skip to content

Commit

Permalink
Improve: transform $_FILES array structure
Browse files Browse the repository at this point in the history
By default PHP creates a weird array structure for $_FILES when working
with arrays. The transformation restores compatibility with the other
superglobals ($_POST etc.)
  • Loading branch information
Trainmaster committed Jul 12, 2014
1 parent 173e3a0 commit 8024927
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Vision/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct()
{
$this->GET = new SquareBracketNotation($_GET);
$this->POST = new SquareBracketNotation($_POST);
$this->FILES = new SquareBracketNotation($_FILES);
$this->FILES = new SquareBracketNotation($this->transformFilesArray($_FILES));
$this->COOKIE = new SquareBracketNotation($_COOKIE);
$this->SERVER = new SquareBracketNotation($_SERVER);

Expand Down Expand Up @@ -364,4 +364,33 @@ protected function initPath()

return $this;
}

/**
* @internal
*
* @param array $files
*
* @return array
*/
protected function transformFilesArray(array $files)
{
foreach ($files as &$value) {
$newArray = array();

foreach ($value as $key => $val) {
if (is_array($val)) {
array_walk_recursive($val, function(&$item) use($key) {
$item = array($key => $item);
});
$newArray = array_replace_recursive($newArray, $val);
}
}

if (!empty($newArray)) {
$value = $newArray;
}
}

return $files;
}
}

0 comments on commit 8024927

Please sign in to comment.