]> git.andy128k.dev Git - ipf.git/commitdiff
sane files structure
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Jan 2015 15:13:21 +0000 (17:13 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Jan 2015 15:13:21 +0000 (17:13 +0200)
ipf/admin/controllers/file_browser.php
ipf/http/request.php

index a65e169366feac3d238a4cd0edfc97e19f9ea20c..ecad3bcf5547c884bd19e68e06f527f6864647da 100644 (file)
@@ -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();
index 0fd8fd37c3d7bbe29a5c833fe71537dfc2837a52..14082e0ff8c10ee32afabfea134cdff316bc0035 100644 (file)
@@ -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;
+    }
 }