php多图像上传脚本不上传一些图像文件

时间:2012-05-06 01:41:12

标签: php image upload image-uploading

我有一个多图像上传脚本,它调整图像大小并创建缩略图。由于一些奇怪的原因,一些图像不会发生。在这种情况下,图像尺寸很小,它是一个JPEG类型的文件。由于某种原因,它没有通过。它与其他图像完美配合。我对单个文件使用相同的脚本,并上传该图像文件。请帮忙!谢谢!

<?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"); ?>

1 个答案:

答案 0 :(得分: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之类的东西让SERVER确定文件的类型。

永远不要相信用户发送给您的内容。