2012-05-06 90 views
1

我有一個多圖像上傳腳本,它調整圖像大小並創建一個縮略圖。由於一些奇怪的原因,一些圖像做的並不是如此。在這種情況下,圖像大小很小,它是一個JPEG類型的文件。由於某種原因,它沒有經過。它與其他圖像完美配合。我對單個文件使用相同的腳本,並上傳該映像文件。請幫忙!謝謝!php多圖像上傳腳本不會上傳一些圖像文件

<?php require_once("../includes/connection.php"); ?> 
<?php require_once("../includes/functions.php"); ?> 
<?php 
$albumName = $_GET['album']; 
$albumDate = $_GET['date']; 
$albumId = $_GET['id']; 
$upload_path = "/home/elevat17/public_html/images/gallery/"; //location 
$images = $_FILES['userFile']['name']; 
$temps = $_FILES['userFile']['tmp_name']; 
$types = $_FILES['userFile']['type']; 
$errors = $_FILES["userFile"]["error"]; 
if ($_FILES["userFile"]["name"]=="") {echo "You must choose a file to upload!";} 
if(in_array("", $images)) {die('Select an image to upload.');} 


else 

{ 
for ($n=0; isset($images[$n]) && isset($temps[$n]) && isset($types[$n]) && isset($errors[$n]); $n++) { 
if ((($types[$n] == "image/gif") 

|| ($types[$n] == "image/jpeg") 

|| ($types[$n] == "image/pjpeg") 

|| ($types[$n] == "image/png") 

|| ($types[$n] == "image/jpg") 

|| ($types[$n] == "image/x-png"))) 


{ 

if ($errors[$n] > 0) 

{ 

$content = "Return Code: " . $errors[$n] . "<br />"; 

} 

else 

{ 

$content = "Upload: " . $images[$n] . "<br />"; 

$content = "Type: " . $types[$n] . "<br />"; 

$content = "<br/><br/>"; 



if (file_exists($upload_path . $images[$n])) 

{ 

die($images[$n].' already exists. Upload cancelled!'); 

} 

else 

{ 

$uploadedfile = $temps[$n]; 

$image = $images[$n]; 

$size = getimagesize($uploadedfile); 

$type = $size['mime']; 

$width = $size[0]; 

$height = $size[1]; 

if($height > '900' || $width > '600') 

{ 

$newwidth=600; // NEW WIDTH 

$newheight=($height/$width)*$newwidth; 

$tmp=imagecreatetruecolor($newwidth,$newheight); 

$filename = $upload_path.$image; 



if($size[2] == IMAGETYPE_GIF) 

    { 

     $src = imagecreatefromgif($uploadedfile); 

     imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

     imagegif($tmp,$filename,100); 

    } 

elseif($size[2] == IMAGETYPE_JPEG) 

    { 

     $src = imagecreatefromjpeg($uploadedfile); 

     imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

     imagejpeg($tmp,$filename,100); 

    } 

elseif($size[2] == IMAGETYPE_PNG) 

    { 

     $src = imagecreatefrompng($uploadedfile); 

     imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

     imagepng($tmp,$filename,9); 

    } 
crop_img(75,75); 
move_uploaded_file($uploadedfile, $filename); 
imagedestroy($src); 

imagedestroy($tmp); 

} 

else 

{  




} 
$query = "INSERT INTO photos (photo_name, in_album) VALUES ('{$image}', $albumId)"; 
      if (mysql_query($query)) {header("location: edit_album.php?id={$albumId}");} 




} 

} 

} 

else { $content  = "Invalid file"; } 

} 

} 

?> 
<?php require("../includes/footer.php"); ?> 
+0

請用更可讀的方式組織您的代碼! – DaneSoul

+0

請提供有關圖像的更多信息。大小,寬度,高度,文件結尾 – Sliq

+0

@ChristianLavie謝謝我認爲你是在正確的軌道上,這是因爲圖像的寬度和高度的代碼是因爲if語句搞砸了。修復。歡呼 –

回答

0

你不檢查上傳是否成功,並假設它已經。這是編寫代碼的不好方法。確切地說,ONE上傳成功,並且很多它失敗的方法。

您還假設上傳的人並非惡意,並且在上傳之前不會將其nastyvirus.exe重命名爲cutekittens.jpg

在最低限度,你需要有

if ($_FILES['userfile']['error'] !== UPLOAD_ERR_OK) { 
    die('Upload failed with error code ' . $_FILES['userfile']['error']); 
} 

,以確保你實際上已經得到了有用的一些工作,然後使用類似http://php.net/fileinfo讓服務器確定文件的類型是什麼。

永遠不會相信用戶發送給您的內容。

+0

非常感謝我解決了這個問題,if語句檢查圖像的高度和寬度是問題所在。我沒有寫代碼,但我太懶惰了,太急於重寫它:P。非常感謝。乾杯 –