2014-10-31 151 views
0

我對圖像使用class.upload.php。調整大小與名稱和擴展名正確工作到文件夾中,但我有一個問題將名稱存儲到MySQL數據庫。沒有文件擴展名(.jpg,.gif等)...爲什麼?我該如何解決這個問題? 感謝class.upload.php和文件擴展名丟失

 /* ========== SCRIPT UPLOAD MULTI IMAGES ========== */ 
    include('class.upload.php'); 
     $dir_dest="../../images/gallery/"; 

    $files = array(); 
    foreach ($_FILES['fleImage'] as $k => $l) { 
    foreach ($l as $i => $v) { 
     if (!array_key_exists($i, $files)) 
      $files[$i] = array(); 
     $files[$i][$k] = $v; 
    } 
} 

foreach ($files as $file) { 

    $handle = new Upload($file); 
     if ($handle->uploaded) { 

    $mainame = $handle->file_dst_name; 

    $db_name = str_replace(" ","_",$mainame); 
    $image1 = md5(rand() * time()) . ".$db_name"; 
    $parts = explode(".",$image1); 
    $extension = end($parts); 
    $result_big = str_replace("." . $extension,"",$image1); 

     $handle->file_new_name_body = $result_big; 
     $handle->image_resize  = true; 
     $handle->image_x   = 460; 
     $handle->image_ratio_y = true; 
     // $handle->image_y   = 400; 
     $handle->Process($dir_dest); 

     //Thumbnail 
    $db_name = str_replace(" ","_",$mainame); 
    $image1 = md5(rand() * time()) . ".$db_name"; 
    $parts = explode(".",$image1); 
    $extension = end($parts); 
    $result_small = str_replace("." . $extension,"",$image1);  

     $handle->file_new_name_body = $result_small; 
     $handle->image_resize  = true; 
     $handle->image_x   = 180; 
     $handle->image_ratio_y = true; 
     // $handle->image_y   = 120; 
     $handle->Process($dir_dest); 

     // we check if everything went OK 
     if ($handle->processed) { 
       header("Location: index.php"); //echo 'image resized'; 
       $handle->clean(); 

    $query_img="INSERT into tbl_images (file_name, pd_image, pd_thumbnail) VALUES('$nome','$result_big', '$result_small')";  
     $result2 = dbQuery($query_img); 

    } else { 
     echo 'error : ' . $handle->error; 
    } 
    } 
    } 
// END SCRIPT UPLOAD MULTI IMAGES 

header("Location: index.php"); 
} 

回答

0

您已經更換使用str_replace

$result_small = str_replace("." . $extension,"",$image1); 

,並在這裏與這裏空字符串進一步擴展

$result_big = str_replace("." . $extension,"",$image1); 

以下線的更新只是在最後

$handle->file_new_name_body = $result_big.$extension; 
添加

$handle->file_new_name_body = $result_small.$extension; 

只是改變這樣的查詢

$query_img="INSERT into tbl_images ( 
       file_name, 
       pd_image, 
       pd_thumbnail 
      ) VALUES (
       '$nome', 
       '{$result_big}.{$extension}', 
       '{$result_small}.{$extension}')"; 

我會建議你有pd_imagepd_thumbnail相同的文件名,只是前綴拇指與thumb_,這將使你的生活更輕鬆前端。

這樣,您只需在thumb_前加上pd_image即可訪問任何圖像縮略圖,而不必在數據庫中存儲pd_thumbnail

+0

它不以這種方式工作。我在文件夾文件上有一個雙點(例如:file..jpg),並且只有一個點進入DB(例如:文件)。 – Ogum 2014-10-31 14:38:18

+0

它已更新並正在工作。請檢查 – Saqueib 2014-10-31 14:57:29

+0

作品的圖像名稱的文件夾,但在數據庫我只有一個點名稱後 – Ogum 2014-10-31 15:47:41

0

您正在使用此代碼刪除擴展程序 $ result_big = str_replace(「。」。$ extension,「」,$ image1);

我不知道你爲什麼這樣做。無論如何,您可以通過在$ handle-> file_new_name_body = $ result_big後面添加以下行來添加它:

$ handle-> file_new_name_ext = $ extension;

+0

$ result_big = $ image1; $ handle-> file_new_name_body = $ result_big; $ handle-> file_new_name_ext = $ extension; 它的工作原理,但我不明白爲什麼我不能看到圖像到文件夾。它沒有擴展名,但在網頁上,我可以看到圖像 – Ogum 2014-10-31 15:37:43

+0

你的意思是一旦$ handle-> Process()運行,它只會創建一個沒有擴展名的文件?例如:如果你上傳abc.png它會創建一個只有abc的文件? btw也試試這個,$ handle-> image_convert ='jpg';這只是爲了調試問題。 通常,當您使用class.upload.php上載文件時,它會創建具有相同名稱和擴展名的文件。你可以通過圖片網址嗎? – Janaka 2014-10-31 15:59:13

+0

是的,它創建一個文件只與名稱..但我可以看到它在前端!對不起,但我正在本地主機上工作 – Ogum 2014-10-31 16:12:57