2016-06-08 50 views
0
$uniqId = $this->input->post('U_I_T_Roll_No'); 
$config['upload_path'] = "./uploads/ProfileImages/"; 
     $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
     $config['max_size'] = '1024'; 
     $config['max_width'] = '1000'; 
     $config['max_height'] = '1000'; 
     /* Load the upload library */ 
     $this->load->library('upload', $config); 
     /* Create the config for image library */ 
     /* (pretty self-explanatory) */ 
     $configThumb = array(); 
     $configThumb['image_library'] = 'gd2'; 
     $configThumb['source_image'] = ''; 
     $configThumb['create_thumb'] = FALSE; 
     $configThumb['maintain_ratio'] = TRUE; 
     /* Set the height and width or thumbs */ 
     /* Do not worry - CI is pretty smart in resizing */ 
     /* It will create the largest thumb that can fit in those dimensions */ 
     /* Thumbs will be saved in same upload dir but with a _thumb suffix */ 
     /* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */ 
     $configThumb['width'] = 400; 
     $configThumb['height'] = 400; 
     /* Load the image library */ 
     $this->load->library('image_lib'); 

    /* We have 5 files to upload 
    * If you want more - change the 6 below as needed 
    */ 
    for($i = 1; $i < 5; $i++) { 
    /* Handle the file upload */ 
    $upload = $this->upload->do_upload('image'.$i); 
    /* File failed to upload - continue */ 
    if($upload === FALSE) continue; 
    /* Get the data about the file */ 
    $data = $this->upload->data(); 

    $uploadedFiles[$i] = $data; 
    /* If the file is an image - create a thumbnail */ 
    if($data['is_image'] == 1) { 
     $configThumb['source_image'] = $data['full_path']; 
     $this->image_lib->initialize($configThumb); 
     $this->image_lib->resize(); 
    } 
    } 
    $S_image = $_FILES['image1']['name']; 
    $F_image = $_FILES['image2']['name']; 
    $M_image = $_FILES['image3']['name']; 
    $LG_image = $_FILES['image4']['name']; 
    $data5=array (
    'Uniq_Id' => $this->input->post('U_I_T_Roll_No'), 

    'S_image' => $S_image, 
    'F_image' => $F_image, 
    'M_image' => $M_image, 
    'LG_unique1' => $LG_image, 
    ); 


    $this->InsertData->studentimageupload($data5); 

上述代碼工作正常。我有4個圖像上傳和正確上傳,圖像保存到數據庫的名稱。問題在於。在上傳過程中重命名文件名

我想根據我上傳圖像名稱並將圖像的名稱保存到數據庫中。

像: US $ uniqId,F $ uniqId,M $ uniqId,LG $ uniqId

回答

0

for循環,只要你do_upload前前$this->load->library('upload', $config);

變化$this->load->library('upload', $config);做這樣的事情來$this->load->library('upload');,看看下面

for($i = 1; $i < 5; $i++) { 
    $img=$_FILES['image'.$i]['name']; 
    $new_name=NEW_NAME; 
    $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1)); 
    $file_name=$new_name.$ext; 
    $config['file_name'] = $file_name; 
$this->upload->initialize($config); 
/* Handle the file upload */ 
$upload = $this->upload->do_upload('image'.$i); 
/* File failed to upload - continue */ 
if($upload === FALSE) continue; 
/* Get the data about the file */ 
$data = $this->upload->data(); 

$uploadedFiles[$i] = $data; 
/* If the file is an image - create a thumbnail */ 
if($data['is_image'] == 1) { 
    $configThumb['source_image'] = $data['full_path']; 
    $this->image_lib->initialize($configThumb); 
    $this->image_lib->resize(); 
    } 
    } 
+0

其實我有4張圖片要上傳。你可以把它放在數組中..以及如何命名所有五個圖像..你只編寫了一個圖像上傳的代碼。我必須寫e次.. –

+0

做同樣的所有5或做一個功能。 – Vinie

+0

查看更新的答案 – Vinie

0

你應該能夠加載庫&後指定 'FILE_NAME' 正確上傳之前文件..

//make your config array. 
$this->load->library('upload', $config); 

for($i=1; $i < 5; $i++) 
{ 
    $this->upload->set_filename($path, $filename); 
    $upload = $this->upload->do_upload('image'. $i); 
    //do whatever you want to do with the file. 
} 

我到目前爲止還沒有在我的任何項目中使用它,但它應該會產生所需的結果。只要讓我知道,如果它不起作用。

+0

請問您可以編輯代碼.. –

+0

我已經用示例代碼更新了我的答案..試試這個。 –

0

使用的名稱是$_FILES['image1']['name']中的任何內容。如果你想改變它,你可以在上傳表單中改變它。如果這是不可取的,你必須調用rename(),然後在數據庫中存儲之前更新

$S_image = $_FILES['image1']['name']; 
$F_image = $_FILES['image2']['name']; 
$M_image = $_FILES['image3']['name']; 
$LG_image = $_FILES['image4']['name']; 

,無論你已經將文件重命名爲。

0

我想你可以在每次調用do_upload方法時通過初始化配置來解決重命名問題。

$config['file_name'] = YOUR_OWN_NAME

,然後初始化您的通話

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

+0

你可以請編輯代碼,並讓我熱點去做。有一個for循環,我不明白如何重命名多個圖像。 –