2015-07-10 91 views
1

我使用下面的代碼上傳codeigniter的多個動態圖像。動態Codeigniter圖像上傳重命名

foreach ($_FILES as $key => $value) { 

    $imagePrefix = time();         

    if (!$this->upload->do_upload($key)) 
    { 
     echo $errors = $this->upload->display_errors(); 
     return ''; 
    } 
    else 
    { 
     $imagename = $imagePrefix.$value['name']; 
     $insertImage = $this->db->query("Insert into `tbl_event_imges` (`iEventID`,`vImage`) values ('".$insertId."','".$imagename."')"); 
    } 
} 

當我上傳一個圖像到特定的文件夾,這工作正常;問題是如果用戶上傳一個具有相同名稱的圖像,那麼它會自動重命名圖像並上傳到該文件夾​​,但我想要的是通過添加$imagePrefix來重命名它,我已經在else中添加了部分代碼,然後想要用這個名字上傳圖片。但這不是與我合作..

有什麼建議嗎?

+0

你得到任何錯誤? –

+0

不,我想重命名使用密鑰重命名上傳文件,,但不工作 – CodeWithCoffee

+0

Codeigniter上傳庫僅適用於單個文件上傳。 – user4419336

回答

0

您需要提供的配置喜好上載功能,像這樣:

$imagePrefix = time(); 
$imagename = $imagePrefix.$value['name']; 

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

$config['upload_path'] = './uploads/'; 
$config['allowed_types'] = 'gif|jpg|png'; 
$config['file_name'] = $imagename; // set the name here 

$this->upload->initialize($config); 

if (!$this->upload->do_upload($key)){ 
    ... 
}else{ 
    ... 
} 

來源:http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload#setting-preferences

+1

這是工作..非常感謝你:) – CodeWithCoffee

0

只是選擇,你可以使用CI的重命名功能文件上傳後重命名。

$image = $this->upload->data(); 
$file_path = $image ['file_path']; 
$file = $image ['full_path']; 
$file_ext = $image['file_ext']; 
$final_file_name = time() . $image['file_name'] . $file_ext; 
0

只添加以下代碼並將其成功運行,也可以設置encrypt_namefalse

$path = $_FILES['userfile']['name']; 
$newName = "Add any name".".".pathinfo($path, PATHINFO_EXTENSION); 
$config['file_name'] = $newName; 
$config['encrypt_name'] = false;