CakePhp上传多个图像

问题描述:

我想保存一个多输入文件,我是从窗体接收的。但结构不是我所期望的...这里是结构:CakePhp上传多个图像

Array 
(
    [files] => Array 
     (
      [name] => Array 
       (
        [0] => 25th birthday.PNG 
        [1] => 1535UDEM-info-2011.jpg 
        [2] => 2012-06-11 14.49.48.jpg 
        [3] => 
       ) 

      [type] => Array 
       (
        [0] => image/png 
        [1] => image/jpeg 
        [2] => image/jpeg 
        [3] => 
       ) 

      [tmp_name] => Array 
       (
        [0] => /tmp/php0GW7d6 
        [1] => /tmp/phpwzS0DO 
        [2] => /tmp/phpMifC4w 
        [3] => 
       ) 

      [error] => Array 
       (
        [0] => 0 
        [1] => 0 
        [2] => 0 
        [3] => 4 
       ) 

      [size] => Array 
       (
        [0] => 159487 
        [1] => 528765 
        [2] => 822193 
        [3] => 0 
       ) 

     ) 

) 

可cakephp保存这种类型的数组?如果是这样,怎么样?我使用这个插件http://cakephp-upload.readthedocs.org上传图片。我想我必须一个一个拯救,但我不知道如何。林试图重新命名每个图像与此代码:

foreach ($imageFiles['name'] as $key => $value) { 
    if(!empty($value)) 
    { 
     $file = $imageFiles['url']; //put the data into a var for easy use 
     $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension 
     $randomCode = substr(md5(uniqid(rand(), true)), 5, 5); 
     $imageFiles['url']['name'] = date('Y-m-d')."_".$randomCode.".".$ext; 
    } else { 
     $imageFiles['url'] = ""; 
    } 

} 

有人可以给我建议如何做到这一点?

但结构是不是我所期待

什么办法? “正常”结构?您需要对数组进行规范化,也不必对其进行规范化,只需对它进行规范化就可以更轻松地处理它,并且只需执行一次foreach并以相同的方式保存每个文件,无论是否存在一个或多个文件。

https://github.com/burzum/cakephp-file-storage/blob/3.0/src/Lib/FileStorageUtils.php

/** 
* Method to normalize the annoying inconsistency of the $_FILE array structure 
* 
* @link http://www.php.net/manual/en/features.file-upload.multiple.php#109437 
* @param array $array 
* @return array Empty array if $_FILE is empty, if not normalize array of Filedata.{n} 
*/ 
    public static function normalizeGlobalFilesArray($array = null) { 
     if (empty($array)) { 
      $array = $_FILES; 
     } 
     $newfiles = array(); 
     if (!empty($array)) { 
      foreach ($array as $fieldname => $fieldvalue) { 
       foreach ($fieldvalue as $paramname => $paramvalue) { 
        foreach ((array)$paramvalue as $index => $value) { 
         $newfiles[$fieldname][$index][$paramname] = $value; 
        } 
       } 
      } 
     } 
     return $newfiles; 
    } 

更好的上传图片或文件的解决方案是Dropzone.js。它非常好用,易于使用,并且还包括用于上传的PHP。

+2

这是一个评论,不是答案也不会这可能解决他与多个文件的问题在一个非常CakePHPish方式。事实上,大多数上传者的PHP脚本吸收,因为他们被认为是例子。 – burzum 2015-03-24 23:46:18