2014-02-06 46 views
0

我有以下的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. 

圖像上傳到服務器,但不創建縮略圖,請幫助。

回答

0

請嘗試上傳「gif」或「png」圖片並檢查它是否會拋出相同的錯誤!

相關問題