簡體   English   中英

CakePhp上傳多個圖像

[英]CakePhp uploading multiple images

我正在嘗試保存從表單接收的多個輸入文件。 但是結構不是我期望的...這是結構:

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM