图片上传到 codeigniter 中的 MySQL 数据库 blob

Image upload to MySQL database blob in codeigniter

我想把图片上传到mysql数据库中,用来存储很多信息。我附上了 3 (MVC) 代码供您参考,请帮助我。

参考:http://forum.codeigniter.com/thread-1205.html

我必须在 codeigniter 中以 blob 类型将许多图像上传到数据库。我写了视图、控制器和模型,所有细节都在上传,但图像单独没有存储。

另请提供如何在 codeigniter 中显示图像。

模型中的

$this->input->post('photo') 无法检索图像信息。因为图像存储在 $_FILES 中而不是 $_POST 中。所以你需要像下面这样在 codeignitor 中使用 upload library

In Controller:

public function update_profile() {
       $id = $this->session->userdata('id');
       $this->load->model('edit_profile_model');

       $config['upload_path'] = './uploads/';
       $config['allowed_types'] = 'gif|jpg|png';
       $config['max_size']  = '100';
       $config['max_width'] = '1024';
       $config['max_height'] = '768';

       $this->load->library('upload', $config);
       $this->upload->do_upload();//upload the file to the above mentioned path
       $this->edit_profile_model->update_db_user_info($id, $this->upload->data());// pass the uploaded information to the model
   } 

In Model:

public function update_db_user_info($id, $imgdata) {
       $imgdata = file_get_contents($imgdata['full_path']);//get the content of the image using its path
       $data = array(
           'fullname' => $this->input->post('fullname'),
           'address' => $this->input->post('address'),
           'state' => $this->input->post('state'),
           'city' => $this->input->post('city'),
           'pincode' => $this->input->post('pincode'),
           'image' => $imgdata,
       );
       $this->db->where('id', $id);
       $this->db->update('userdetails', $data);
   } 

要检索图像,请在模型中编写一个函数,如下所示。

public function get_image($id){
       $this->db->where('id', $id);
       $result = $this->db->get('userdetails');
       header("Content-type: image/jpeg");
       echo $result['image'];
}

而且存储图像并从数据库中检索也不是一个好习惯。而不是尝试将图像存储在文件夹中并将路径存储在数据库中,如下所示。

In Model:

public function update_db_user_info($id, $imgdata) {
       $imgdata = $imgdata['full_path'];// get the path of the image
       $data = array(
           'fullname' => $this->input->post('fullname'),
           'address' => $this->input->post('address'),
           'state' => $this->input->post('state'),
           'city' => $this->input->post('city'),
           'pincode' => $this->input->post('pincode'),
           'image' => $imgdata,// change the type of image from blob to varchar or text
       );
       $this->db->where('id', $id);
       $this->db->update('userdetails', $data);
   }