PHP Image Uploader - 截断数组后期处理?

时间:2013-02-22 22:59:02

标签: php arrays

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

在我获得$ _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
        }

对不起那里的所有echo语句,试图弄清楚发生了什么。当我尝试上传某些内容时(来自Firebug): enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

尝试更改:

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

$file_array = organizeFilesArray($_FILES);