From 8024927df5ee3f04460892d67c120641625f15e7 Mon Sep 17 00:00:00 2001 From: Frank Liepert Date: Sat, 12 Jul 2014 21:42:01 +0200 Subject: [PATCH] Improve: transform $_FILES array structure By default PHP creates a weird array structure for $_FILES when working with arrays. The transformation restores compatibility with the other superglobals ($_POST etc.) --- src/Vision/Http/Request.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Vision/Http/Request.php b/src/Vision/Http/Request.php index 8c080bc..efbac45 100644 --- a/src/Vision/Http/Request.php +++ b/src/Vision/Http/Request.php @@ -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); @@ -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; + } }