2015-10-13 71 views
2

到目前爲止,總是爲數據庫中的圖像保存路徑,但是這次我必須將實際圖像保存在數據庫中。我在我的表準備一個字段(LONGBLOB)如何使用codeigniter在mysql中保存圖像?

我有這樣的代碼在我的控制器:

$insertWhat = array(

    'dsrequestFileName1' => $this->input->post('dsrequestFileName1'), 
    'dsrequestFile1'  => isset($_FILES['dsrequestFile1']['name'])? $_FILES['dsrequestFile1']['name'] : '', 
    'dsrequestFileName2' => $this->input->post('dsrequestFileName2'), 
    'dsrequestFile2'  => isset($_FILES['dsrequestFile2']['name'])? $_FILES['dsrequestFile2']['name'] : '' 
); 

$now = date('Y-m-d-His'); 
$valid_extensions = array('gif', 'jpg', 'jpeg', 'png', 'bmp',''); 
if($insertWhat['dsrequestFile1'] !=""){  
    if (!in_array(end(explode('.', strtolower($_FILES['dsrequestFile1']['name']))), $valid_extensions)) { 
     $this->session->set_flashdata('wrongtype', '<p class=error>Wrong type of file has been uploaded! Only gif, jpg, bmp and png are allowed.</p>'); 
     redirect('somepage/somefunctionHere'); 
    } 
    $insertWhat['dsrequestFile1'] = str_replace(' ', '_',$insertWhat['dsrequestFile1']); 
    $insertWhat['dsrequestFile1'] = $now.$insertWhat['dsrequestFile1']; 
} 

$config['upload_path'] = 'somefolder/imagesfolder'; 
$config['allowed_types'] = 'gif|jpg|jpeg|bmp|png'; 
$config['max_size'] = '2048'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768'; 
$config['file_name'] = $insertWhat['dsrequestFile1']; 
$this->load->library('upload', $config); 
$this->upload->initialize($config); 
// perform the upload 
$this->upload->do_upload('dsrequestFile1'); 
$data = array('upload_data' => $this->upload->data('dsrequestFile1')); 
// code that saves the insert data is irrelevant to the question, I can post it here if needed however. 

然而,這個代碼只上傳圖片。我應該在這裏更改什麼,以便將圖像保存在數據庫中。如果可能的話,我不想上傳圖片。

不幸的是,在CI手冊上沒有任何內容。

+0

重複。請檢查此鏈接http://stackoverflow.com/questions/9737384/code-igniter-how-to-save-images-into-database –

+0

使用查詢來做到這一點。上傳後取文件名,保存在數據庫中。 –

+0

我沒有檢查那個頁面,但是那裏提供的鏈接被破壞了,並且解決方案不起作用。 – user2417624

回答

3

你好,請試試這個代碼

if($this->input->post('update_list')) 
    { 
     if($this->upload->do_upload('profile_pic')) 
     { 
      $image_file = array('upload_data' => $this->upload->data()); 
      $img_name=file_get_contents($image_file); /* This function move to image in your database*/ 


      $this->your_mode->add_image_table($img_name);// pass image as parameter to your model 
     } 

    } 
+1

謝謝,你的回答對我很有幫助。 –