2013-04-24 77 views
3

我知道這個問題已被問了好幾次,但我發現的所有其他問題都不同於我想要的。如何上載圖像路徑和名稱到數據庫 - Codeigniter

我想上傳文件名和文件路徑到一個名爲'factoryimages'的表。

這樣做的最好方法是什麼?

我的控制器功能:

function do_upload() 
{ 
    $config['upload_path'] = './assets/uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = ''; 
    $config['max_height'] = ''; 
    $config['overwrite'] = TRUE; 
    $config['remove_spaces'] = TRUE; 

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

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

     $this->load->view('members/header'); 
     $this->load->view('members/upload_form', $error); 
     $this->load->view('members/footer'); 
    } 
    else 
    { 
     $data = array('upload_data' => $this->upload->data()); 
     $this->load->view('members/header'); 
     $this->load->view('members/upload_success', $data); 
     $this->load->view('members/footer'); 
    } 
} 

我想這在我的模型,但它沒有工作:

function insert_images() 
{ 
    $insert_data = array(
    'filename' => $image_data['file_name'], 
    'fullpath' => $image_data['full_path'] 
    ); 

    $this->db->insert('bedrijfimages', $insert_data); 
} 

我的觀點:

<?php echo $error;?> 
<br/> 
<?php echo form_open_multipart('upload/do_upload');?> 

<input type="file" name="userfile" size="20" multiple="true" /> 

<br /><br /> 

<input type="submit" value="upload" /> 

</form> 

編輯:

我得到一個服務器錯誤,但ic你看不到它是什麼。

我的控制器功能:

function do_upload() 
{ 
    $config['upload_path'] = './assets/uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = ''; 
    $config['max_height'] = ''; 
    $config['overwrite'] = TRUE; 
    $config['remove_spaces'] = TRUE; 

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

    if (! $this->upload->do_upload()) 
    { 
     $error = array('error' => $this->upload->display_errors()); 
     $this->upload_model->insert_images($this->upload->data()); 
     $this->load->view('members/header'); 
     $this->load->view('members/upload_form', $error); 
     $this->load->view('members/footer'); 
    } 
    else 
    { 
     $data = array('upload_data' => $this->upload->data()); 
     $this->load->view('members/header'); 
     $this->load->view('members/upload_success', $data); 
     $this->load->view('members/footer'); 
    } 
} 

我的模型功能:

function insert_images($data = array()) 
{ 
    $data = array(
    'filename' => $image_data['file_name'], 
    'fullpath' => $image_data['full_path'] 
    ); 

    $this->db->insert('bedrijfimages', $data); 
} 

可能是什麼問題呢?

回答

3

必須將此數據傳遞到您的插入爲陣列$this->upload->data()

在你的控制器,你必須在驗證完成後

else 
{ 
    $this->MODELNAME->insert_images($this->upload->data()); 
    $data = array('upload_data' => $this->upload->data()); 
    $this->load->view('members/header'); 
    $this->load->view('members/upload_success', $data); 
    $this->load->view('members/footer'); 
} 

而在你的模型設置:

function insert_images($image_data = array()){ 
     $data = array(
      'filename' => $image_data['file_name'], 
      'fullpath' => $image_data['full_path'] 
    ); 
     $this->db->insert('bedrijfimages', $data); 
} 

你可以請查看CI文檔頁面中此數組內的信息:​​

+0

我收到服務器錯誤。請看看我剛添加到問題中的代碼。 – 2013-04-24 11:58:33

+0

我更新了我的答案。請注意,MODELNAME必須是您的圖像模型的名稱,您具有inserт_images功能 – Svetoslav 2013-04-24 12:03:40

+0

工作正常!謝謝。 – 2013-04-24 12:05:29

相關問題