简体   繁体   English

CakePhp上传多个图像

[英]CakePhp uploading multiple images

I'm trying to save a multiple input file I´m receiving from a form. 我正在尝试保存从表单接收的多个输入文件。 But the structure is not what I expected... Here is the structure: 但是结构不是我期望的...这是结构:

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
                )

        )

)

Could cakephp save this type of array? Cakephp可以保存这种类型的数组吗? If so, how? 如果是这样,怎么办? I´m using http://cakephp-upload.readthedocs.org this plugin to upload the image. 我正在使用http://cakephp-upload.readthedocs.org这个插件上传图片。 I think I must save one by one, but I´m not sure how to. 我认为我必须一一保存,但我不确定该如何保存。 Im trying to rename each image with this code: 我试图用以下代码重命名每个图像:

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'] = "";
    }

}

Could someone give me advice on how to do this? 有人可以给我一些建议吗?

But the structure is not what I expected 但是结构不是我所期望的

What did you expect? 您期望什么? The "normal" structure? “正常”结构? You need to normalize the array, well don't have to but normalizing it makes working with it a lot easier and you can just do a foreach and save each file the same way, no matter if one or many are present. 您需要对数组进行规范化,而不必这样做,但是对其进行规范化可以使它的使用变得更加容易,并且您可以执行一个foreach并以相同的方式保存每个文件,而不管是否存在一个或多个。

See https://github.com/burzum/cakephp-file-storage/blob/3.0/src/Lib/FileStorageUtils.php 参见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;
    }

A better solution to Upload images or Files is Dropzone.js . Dropzone.js是上传图片或文件的更好解决方案。 Its really nice ans easy to use and also includes PHP for the Upload. 它非常好用且易于使用,还包括用于上传的PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM