2009-12-30 70 views
0

我有以下模型(codeigniter)代碼上傳圖像和縮略圖。上傳PHP的問題:縮略圖擷取圖像名稱

但是圖像路徑的結果變成了images/comfort_big.jpg和images/comfort_big.jpg.jpg。

縮略圖圖像顯示圖像的名稱並添加.jpg。

我上傳images/confort_thumb.jpg爲拇指。

有誰能告訴我有什麼問題嗎?

if ($_FILES){ 
     $config['upload_path'] = './images/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '200'; 
     $config['remove_spaces'] = true; 
     $config['overwrite'] = false; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 

     $this->load->library('upload', $config);  
     if (strlen($_FILES['image']['name'])){ 
      if(!$this->upload->do_upload('image')){ 
       $this->upload->display_errors(); 
       exit(); 
      } 
      $image = $this->upload->data(); 
      if ($image['file_name']){ 
       $data['image'] = "images/".$image['file_name']; 
      } 
     } 

     if (strlen($_FILES['thumbnail']['name'])){ 
      if(!$this->upload->do_upload('thumbnail')){ 
       $this->upload->display_errors(); 
       exit(); 
      } 
      $thumb = $this->upload->data(); 
      if ($thumb['file_name']){ 
       $data['thumbnail'] = "images/".$thumb['file_name']; 
      } 
     } 
    } 
+1

請注意:爲什麼您要讓用戶同時上傳完整尺寸的圖片和縮略圖,用戶是否更喜歡從全尺寸圖片自動生成縮略圖? – 2009-12-30 11:16:12

回答

1

我有用戶指南
在它規定的偏好表中快速瀏覽一下File Uploading Class

注意:文件名不能包含文件擴展名。

所以我的猜測是,你讓用戶將文件命名

$config['file_name'] = "User defined"; 

或編程方式刪除的擴展。既然你知道並列出了擴展已經你可以做

$types = array(".gif",".jpg",".png"); 
$image['file_name'] = str_replace($types , "", $image['file_name']); 
0

你的問題是,當你創建一個拇指你傳遞完整的形象。

因此它會添加另一個擴展名。

使用下面的代碼從文件名中刪除擴展名。

<?php 
    $filename=$image['file_name']; 

    $file_name_with_dot = substr($filename,0, strrpos($filename, '.') + 1); 

    $only_file_name = substr($file_name_with_dot,0,strlen($file_name_with_dot)-1); 
?> 

現在這個$only_file_name變量傳遞給你的拇指功能..

這將是更好地使用來自主圖像自動生成縮略圖。

希望這對你有幫助。