From c7f8a3addc416ee03268f6e420d6718222b480cf Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 17 Jan 2015 17:13:21 +0200 Subject: [PATCH] sane files structure --- ipf/admin/controllers/file_browser.php | 7 +++---- ipf/http/request.php | 28 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/ipf/admin/controllers/file_browser.php b/ipf/admin/controllers/file_browser.php index a65e169..ecad3bc 100644 --- a/ipf/admin/controllers/file_browser.php +++ b/ipf/admin/controllers/file_browser.php @@ -166,10 +166,9 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller function upload() { - $count = count($this->request->FILES['files']['name']); - for ($i = 0; $i < $count; ++$i) { - $uploadfile = $this->dir . basename($this->request->FILES['files']['name'][$i]); - move_uploaded_file($this->request->FILES['files']['tmp_name'][$i], $uploadfile); + foreach ($this->request->FILES['files'] as $file) { + $uploadfile = $this->dir . basename($file['name']); + move_uploaded_file($file['tmp_name'], $uploadfile); } return $this->backToIndex(); diff --git a/ipf/http/request.php b/ipf/http/request.php index 0fd8fd3..14082e0 100644 --- a/ipf/http/request.php +++ b/ipf/http/request.php @@ -22,7 +22,7 @@ class IPF_HTTP_Request $this->GET =& $_GET; $this->REQUEST =& $_REQUEST; $this->COOKIE =& $_COOKIE; - $this->FILES =& $_FILES; + $this->FILES = self::saneFiles($_FILES); $this->method = $_SERVER['REQUEST_METHOD']; $this->uri = $_SERVER['REQUEST_URI']; $this->remote_addr = $_SERVER['REMOTE_ADDR']; @@ -69,5 +69,31 @@ class IPF_HTTP_Request { return array_merge_recursive($this->POST, $this->FILES); } + + public static function saneFiles($files) + { + $result = array(); + foreach ($files as $prefix => $v) { + foreach ($v as $key => $array) { + array_walk_recursive($array, function(&$v, $i) use($key) { + $v = array($key => $v); + }); + $result = self::deepMerge($result, array($prefix => $array)); + } + } + return $result; + } + + private static function deepMerge($into, $arr) + { + foreach ($arr as $key => $value) { + if (array_key_exists($key, $into) && is_array($into[$key]) && is_array($value)) { + $into[$key] = self::deepMerge($into[$key], $value); + } else { + $into[$key] = $value; + } + } + return $into; + } } -- 2.49.0