简体   繁体   English

将图像上传到Codeigniter中的MySQL数据库Blob

[英]Image upload to MySQL database blob in codeigniter

I want to upload the image to the mysql database for storing many information. 我想将图像上传到mysql数据库以存储许多信息。 I have attached the 3 (MVC) code for your reference and please help me. 我随附了3(MVC)代码供您参考,请帮助我。

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

I have to upload many images to database in blob type in codeigniter. 我必须在codeigniter中以blob类型将许多图像上传到数据库。 I have written view, controller and model all details are uploading but image alone not storing. 我已经写过视图,控制器和模型的所有详细信息都已上传,但仅图像未存储。

Also please provide how to show the image in the codeigniter. 还请提供如何在codeigniter中显示图像。

$this->input->post('photo') in model won't work to retrieve the image information. $this->input->post('photo')在模型中将无法检索图像信息。 Because the images are stored in $_FILES not in $_POST. 因为图像存储在$ _FILES中而不是$ _POST中。 So you need to use the upload library in codeignitor like below. 因此,您需要在codeignitor中使用上载库 ,如下所示。

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);
   } 

To retrieve the image write a function in the model like below. 要检索图像,请在下面的模型中编写一个函数。

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

And also its not a good practice to store the image and retrieving from database. 而且,存储图像并从数据库检索也不是一个好习惯。 Instead of that try to store the image in a folder and store the path in the database like below. 与其尝试将图像存储在文件夹中,然后将路径存储在数据库中,如下所示。

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);
   } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM