2016-11-20 95 views
0

我想如果一個文件被從輸入文件標籤文件上傳與添加鏈接到數據庫

<input name="file1" type="file" id="addimage1"> 

我想知道如何處理它在車型選擇上載文件在一個特定的文件。我想將圖像的鏈接發送到數據庫。

當我不使用代碼點火我這樣做:

if(!empty($_FILES['file1']['name'])) { 
    move_uploaded_file($_FILES["file1"]["tmp_name"],'assets/results/'.$myid.'/'. $_FILES["file1"]["name"]); 
    $link1='assets/results/'.$myid .'/' . $_FILES["file1"]["name"];  
} 

我怎樣才能做到這一點的代碼點火器。

+0

閱讀文檔https://www.codeigniter.com/userguide3/libraries/file_uploading.html –

回答

1

隨着用戶指南指出:

//after uploading config 
if (! $this->upload->do_upload('userfileinputname')) 
       { 
         $error = array('error' => $this->upload->display_errors()); 
         $this->load->view('upload_form', $error); 
       } 
       else 
       { 
         $data = array('upload_data' => $this->upload->data()); 
         //here add to db 
         $this->file_model->add_link_to_db($data['filename']); 

         $this->load->view('upload_success', $data); 
       } 
} 

而且你的模型中:

public function add_link_to_db($filename) { 
    return $last_id = $this->db->insert('mytable', ['name'=>$filename]); 
}