0

好吧,我正在为一些客户构建一个基本的图像上传器,我遇到了某种错误,我正在撕扯我的头发。

在我得到 $_FILES 数组后,我做了一些处理,以便更容易解析图像,但后处理我的值被截断。

这是相关的PHP:

function organizeFilesArray(&$file_post) {

    $file_array = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_array[$i][$key] = $file_post[$key][$i];
            echo "$file_array[$i][$key]";
            echo " ... ";
            echo "$file_post[$key][$i]    ";
        }
        echo "$file_count ";

    }

    return $file_array;
}

if(isset($_POST)){
    $destinationDirectory = '/Applications/MAMP/htdocs/image_upload/uploads/';

    //check if _FILES array is empty or if the file was uploaded via POST
    if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])){
        die('Something went wrong with Upload!');
    }

    //clean up the array so I can handle it simply
    //$imageArray = organizeFilesArray($_FILES['ImageFile']);
    $file_array = organizeFilesArray($_FILES['ImageFile']);
    echo" array size pp: " . sizeof($file_array);
    foreach($file_array as $file){
        //random number to be added after image name
        $randomNumber   = rand(0, 9999999999);

        //access these values by using their index position
        //$imageName  = str_replace(' ','-',strtolower($file['name']));
        $imageName  = $file['name']; //get file name
        $imageSize  = $file['size']; // Obtain original image size
        $tempSrc    = $file['tmp_name']; // Tmp name of image file stored in PHP tmp folder
        $imageType  = $file['type']; //Obtain file type, returns "image/png", image/jpeg, text/plain etc.

        echo "This is the type before we try: " . $file['type'];
        echo "This is the name before we try: " . $file['name'];
        echo "This is the size before we try: " . $file['size'];
        //make sure filetype is supported
        switch(strtolower($imageType)){
            case 'image/png':
                $createdImage =  imagecreatefrompng($file['tmp_name']);
                break;
            case 'image/gif':
                $createdImage =  imagecreatefromgif($file['tmp_name']);
                break;
            case 'image/jpeg':
            case 'image/pjpeg':
                $createdImage = imagecreatefromjpeg($file['tmp_name']);
                break;
            default:
                die('Unsupported File!:'.$imageType); //output error and exit
        }

很抱歉那里的所有回声语句,试图弄清楚发生了什么。当尝试上传一些东西时,我得到了这个(来自 Firebug): 在此处输入图像描述

所以.. 处理后,我数组中的所有内容都被截断为第一个字符?任何帮助将不胜感激!

4

1 回答 1

1

看起来您可能只是分解了一个上传的文件。

尝试改变:

$file_array = organizeFilesArray($_FILES['ImageFile']);

只是

$file_array = organizeFilesArray($_FILES);
于 2013-02-23T00:28:16.783 回答