3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
您可以在上傳庫(kù)的配置中將時(shí)間附加到“文件名”
$config['file_name'] = time().$_FILES['userfile1']['name'];
或者,如果您想要所有文件的唯一名稱(chēng),只需添加
$config['encrypt_name'] = TRUE;
然后
$this->load->library('upload', $config);

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊
試試這個(gè):-
$path = pathinfo($_FILES["userfile1"]["name"]);
$image_path = $path['filename'].'_'.time().'.'.$path['extension'];

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
我已經(jīng)為您的代碼編寫(xiě)了一個(gè)可能的解決方案。您還沒(méi)有共享完整的代碼,因此您必須自己填補(bǔ)空白,并且可能會(huì)在這里或那里進(jìn)行一些更改;必要時(shí)會(huì)提及評(píng)論。看看它是否對(duì)你有幫助。
public function your_function_name(){
// your-code
// your-code
// check if file is uploaded in field1
if(!empty($_FILES['userfile1']['name'])){
// call function to upload file
$userfile1 = $this->upload_file('userfile1');
}
// check if file is uploaded in field2
if(!empty($_FILES['userfile2']['name'])){
$userfile2 = $this->upload_file('userfile2');
}
// check if file is uploaded in field3
if(!empty($_FILES['userfile3']['name'])){
$userfile3 = $this->upload_file('userfile3');
}
$result = $this->post_model->adregister($userfile1, $userfile2, $userfile3);
}
// function to upload file
function upload_file($filename){
$config['file_name'] = time().$_FILES[$filename]['name']; // append time to filename
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
$config['max_size'] = 5120;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$uploaded = $this->upload->do_upload($filename);
if ( ! $uploaded ){
$error = array('error' => $this->upload->display_errors());
$file = 'no_image.jpg'; // default file
}else{
$upload_data = $this->upload->data();
$file = $upload_data['file_name']; // uploaded file name
}
return $file; // return filename
}
- 3 回答
- 0 關(guān)注
- 256 瀏覽
添加回答
舉報(bào)