PHP图片上传问题

时间:2010-09-09 16:31:30

标签: php

我无法获取以下代码来上传大图片。它适用于小于1000像素×1000像素的图像,但在任何更大的图像上都可以打破。任何帮助/想法非常感谢。

注意:我已经尝试将'$ memoryNeeded> = 10000000'增加到'7700000000',但仍然没有乐趣。

if (!$error && is_uploaded_file($_FILES['galleryFile']['tmp_name'])) {
        $format = strtolower(substr(strrchr($_FILES['galleryFile']['name'],"."),1));            
        $str = strtolower(trim($_FILES['galleryFile']['name']));
        $str = preg_replace('/[^a-z0-9-]/', '-', $str);
        $str = preg_replace('/-+/', "-", $str);    
        $filename=$str.'.'.$format;                
        $uploadGallery=$origFileDir.$filename;
        foreach ($allowedImgFormats as $key => $value) {
            $value==$format ? $imgFormatOK='1' : NULL;
        }
        $imgFormatOK=='0' ? $error='You are attempting to upload an image with an invalid format!<br />Please only upload images with ".gif", ".jpg" or ".jpeg" extensions.' : NULL;
        if (!$error && move_uploaded_file($_FILES['galleryFile']['tmp_name'], $uploadGallery)){ 
            $galleryW='944'; $galleryH='733';                        
            $galleryInfo = getimagesize($uploadGallery);
            $memoryNeeded = Round(($galleryInfo[0] * $galleryInfo[1] * $galleryInfo['bits'] * $galleryInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
            if ($memoryNeeded>=10000000) {
                unlink($uploadGallery); $error='The chosen image is too large to process.<br />Please upload a smaller image (lower dimensions and/or resolution).';
            } else {
                list($wOrig, $hOrig) = getimagesize($uploadGallery);
                $ratio_orig = $wOrig/$hOrig;
                if ($wOrig > $galleryW) { $galleryW = $galleryH*$ratio_orig; $galleryH = $galleryW/$ratio_orig; } else { $galleryW=$wOrig; $galleryH=$hOrig; }    
                if ($galleryH > $galleryH) { $galleryH = $galleryW*$ratio_orig; $galleryW = $galleryH/$ratio_orig; }
                $galleryP = imagecreatetruecolor($galleryW, $galleryH); 

                switch($format) {
                   case 'gif' : $thisGallery = imagecreatefromgif($uploadGallery); break;
                   case 'jpg' : $thisGallery = imagecreatefromjpeg($uploadGallery); break;
                }
                imagecopyresampled($galleryP, $thisGallery, 0, 0, 0, 0, $galleryW, $galleryH, $wOrig, $hOrig);

                switch($format) {
                   case 'gif' : $createGallery=imagegif($galleryP, $galleryFileDir.$filename, 88); break;
                   case 'jpg' : $createGallery=imagejpeg($galleryP, $galleryFileDir.$filename, 88); break;
                }
                imagedestroy($galleryP); imagedestroy($thisGallery); unlink($uploadGallery);

                if (!$createGallery) {
                     $error='The chosen image failed to transfer correctly.<br />Please try again, or attempt to upload an alternative image.';
                     file_exists($galleryFileDir.'/'.$filename) ? unlink($galleryFileDir.'/'.$filename) : NULL;
                } else {            
                    $_POST['imagename']=$filename; 
                    mysql_query("INSERT INTO isgallery(imagename, assoc_object) VALUES('".$_POST['imagename']."', '".$_POST['id']."')");                         
                }
            }        
        } else {
            !$error ? $error='The chosen image failed to upload correctly.<br />Please try again, or attempt to upload an alternative image.' : NULL;
            file_exists($uploadGallery) ? unlink($uploadGallery) : NULL;
        }
    }

1 个答案:

答案 0 :(得分:0)

如果您处理的是真彩色,则1000x1000图像需要 LEAST 3,000,000字节的内存。如果您正在进行Alpha透明度,则为4,000,000。如果$memoryNeeded变量设置为大于PHP memory_limit的变量,则它是无用的。它会愉快地尝试创建一个图像并因超出限制而失败。

您可以查看ini_get('memory_limit')的限制,尽管您很可能无法在没有按摩的情况下直接使用此值进行计算,因为它可能会返回类似'32M'的内容(32兆字节限制),而不是33554432