2012-05-06 3 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 방법이 있으며, 성공하려면 MANY 방법이 있습니다.

또한 업로드하는 사람이 악의적이지 않으며 업로드하기 전에 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. 정말 고마워. 건배 –

관련 문제