我有以下用于在我的网站上传图像的PHP代码。
//代码
global $objDB;
global $path_to_image_directory ;
global $path_to_thumbs_directory ;
$final_width_of_image = 500;
$path_to_image_directory = 'uploads/fullsized/';
$path_to_thumbs_directory = 'uploads/thumbs/';
$imagetitle="";
$mysqli = $objDB->connection;
if(isset($_FILES['uploaded']['name']))
{
if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['uploaded']['name'])) {
$filename = $_FILES['uploaded']['name'];
$source = $_FILES['uploaded']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
createThumbnail($filename);
$sql="INSERT INTO gallery(image)VALUES('$filename')";
$result=$objDB->query($sql);
if($result==1)
{
header("location:gallery.php?s_msg=Image added To gallery");
}
else
{
header("location:gallery.php?f_msg=Unable to Add Image");
}
}
else
{
header("location:gallery.php?f_msg=Unable to Add Image");
}
}
else
{
header("location:gallery.php?f_msg=Unable to Add Image");
}
function createThumbnail($filename) {
$path_to_image_directory="";
$path_to_thumbs_directory="";
$final_width_of_image="";
$path_to_image_directory = 'uploads/fullsized/';
$path_to_thumbs_directory = 'uploads/thumbs/';
$final_width_of_image = 400;
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $filename);
}
直到昨天,当用户尝试上传大小约为1MB的图片时,一切正常。
并且正在抛出错误:
Premature end of JPEG file in db_gallery.php on line 64
fullsized/the_amazing_spider_man_2_movie-2048x1536.jpg' is not a valid JPEG file.
图像上传到服务器但未创建缩略图,请使用Pls帮助。
答案 0 :(得分:0)
请尝试上传“gif”或“png”图片并检查是否会引发同样的错误!