2012-02-07 114 views
2

我爲我的一個項目使用CI 2.1.0和mysql數據庫。我正在使用我的圖片上傳方法遇到問題。我上傳的圖像應保存在上傳目錄中,並創建圖像的縮略圖版本,圖像路徑應保存在數據庫中。 我已經完成的代碼工作正常,但有一個問題,當我上傳圖片時,在上傳目錄中,我獲得了同一圖片的兩個副本,並在拇指目錄中顯示了上傳圖片的單個副本。我想只有一個圖像副本而不是這兩個副本。 這裏是我的代碼 - >
codeigniter中的圖像上傳問題2.1.0

型號:

function do_upload() //to upload images in upload directory 
     { 
      $i=$this->db->get('portfolio')->num_rows(); 
      $i=$i+1; 
      $image_path=realpath(APPPATH . '../uploads'); 
      $config=array(
       'allowed_types'=>'jpeg|png|gif|jpg', 
       'upload_path'=>$image_path, 
       'max_size'=>2097152, 
       'file_name'=>'_'.$i.'_' 
      ); 
      $this->load->library('upload', $config); 
      $this->upload->do_upload(); 
      $image_data = $this->upload->data(); 
      $config=array(
       'source_image'=>$image_data['full_path'], 
       'new_image'=>$image_path.'/thumbs', 
       'maintain_ration'=>TRUE, 
       'width'=>150, 
       'height'=>100 
      ); 
      $this->load->library('image_lib', $config); 
      $this->image_lib->resize(); 
      if(! $this->upload->do_upload()) 
      { 
       $error = array('error' => $this->upload->display_errors()); 
       return $error; 
      } 
      else 
      { 
       return $image_data; 
      } 
     } 


一些請告訴我爲什麼圖像的兩個副本被上傳。 還有一件事情,如果存在同名圖像,我希望圖像被覆蓋。我已經改變了upload.php文件中system->libraries這個

public $overwrite    = TRUE; 

,但它無法正常工作。有人請幫忙。

回答

1

你調用$這個 - > upload-> do_upload()兩次..

請試試這個代碼

警告:未經測試

function do_upload() 

     { 
      $i=$this->db->get('portfolio')->num_rows(); 
      $i=$i+1; 

      $image_path=realpath(APPPATH . '../uploads'); 

      $config=array(
       'allowed_types'=>'jpeg|png|gif|jpg', 
       'upload_path'=>$image_path, 
       'max_size'=>2097152, 
       'overwrite'=>TRUE, 
       'file_name'=>'_'.$i.'_' 
      ); 


      $this->load->library('upload', $config); 


     if(! $this->upload->do_upload()) 
      { 
       $error = array('error' => $this->upload->display_errors()); 
       return $error; 
      } 
      else 
      { 

     $image_data = $this->upload->data(); 
      $config=array(
       'source_image'=>$image_data['full_path'], 
       'new_image'=>$image_path.'/thumbs', 
       'maintain_ration'=>TRUE, 
       'width'=>150, 
       'height'=>100 
      ); 
      $this->load->library('image_lib', $config); 
      $this->image_lib->resize(); 
       return $image_data; 
      } 
     } 
+0

謝謝:) @krish – Shabib 2012-02-07 09:28:29

1

我會給備用上傳用於正確處理文件上傳的類。您可以在任何地方重新使用此代碼。

<?php 

//Save file as Uploader.php 
//File Uploading Class 

class Uploader 
{ 
private $destinationPath; 
private $errorMessage; 
private $extensions; 
private $allowAll; 
private $maxSize; 
private $uploadName; 
private $seqnence; 
public $name='Uploader'; 
public $useTable =false; 

function setDir($path){ 
$this->destinationPath = $path; 
$this->allowAll = false; 
} 

function allowAllFormats(){ 
$this->allowAll = true; 
} 

function setMaxSize($sizeMB){ 
$this->maxSize = $sizeMB * (1024*1024); 
} 

function setExtensions($options){ 
$this->extensions = $options; 
} 

function setSameFileName(){ 
$this->sameFileName = true; 
$this->sameName = true; 
} 
function getExtension($string){ 
$ext = ""; 
try{ 
$parts = explode(".",$string); 
$ext = strtolower($parts[count($parts)-1]); 
}catch(Exception $c){ 
$ext = ""; 
} 
return $ext; 
} 

function setMessage($message){ 
$this->errorMessage = $message; 
} 

function getMessage(){ 
return $this->errorMessage; 
} 

function getUploadName(){ 
return $this->uploadName; 
} 
function setSequence($seq){ 
$this->imageSeq = $seq; 
} 

function getRandom(){ 
return strtotime(date('Y-m-d H:iConfused')).rand(1111,9999).rand(11,99).rand(111,999); 
} 
function sameName($true){ 
$this->sameName = $true; 
} 
function uploadFile($fileBrowse){ 
$result = false; 
$size = $_FILES[$fileBrowse]["size"]; 
$name = $_FILES[$fileBrowse]["name"]; 
$ext = $this->getExtension($name); 
if(!is_dir($this->destinationPath)){ 
$this->setMessage("Destination folder is not a directory "); 
}else if(!is_writable($this->destinationPath)){ 
$this->setMessage("Destination is not writable !"); 
}else if(empty($name)){ 
$this->setMessage("File not selected "); 
}else if($size>$this->maxSize){ 
$this->setMessage("Too large file !"); 
}else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){ 

if($this->sameName==false){ 
$this->uploadName = $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext; 
}else{ 
$this->uploadName= $name; 
} 
if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName)){ 
$result = true; 
}else{ 
$this->setMessage("Upload failed , try later !"); 
} 
}else{ 
$this->setMessage("Invalid file format !"); 
} 
return $result; 
} 

function deleteUploaded(){ 
unlink($this->destinationPath.$this->uploadName); 
} 

} 

?> 

使用Uploader.php

<?php 

$uploader = new Uploader(); 
$uploader->setDir('uploads/images/'); 
$uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list// 
$uploader->setMaxSize(.5); //set max file size to be allowed in MB// 

if($uploader->uploadFile('txtFile')){ //txtFile is the filebrowse element name // 
$image = $uploader->getUploadName(); //get uploaded file name, renames on upload// 

}else{//upload failed 
$uploader->getMessage(); //get upload error message 
} 


?>