I have the following php code for uploading images on my website.
//codes
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);
}
Everything was working fine till yesterday, when user tried to upload an image of size about 1MB
and is throwing error :
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.
The image is uploaded to server but thumbnail is not created, Pls Help.
请尝试上传“ gif”或“ png”图片,并检查是否会引发相同的错误!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.