0

I want to upload the image to the mysql database for storing many information. I have attached the 3 (MVC) code for your reference and please help me.

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

I have to upload many images to database in blob type in codeigniter. 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
VPR
  • 77
  • 2
  • 3
  • 13
  • Why do you need to store images in database at all? That is typically a bit of an anti-pattern. Also, add your code to the question and explicitly indicate where you are having problems. – Mike Brant Feb 20 '15 at 04:10
  • then what is the good pratice .. if some hacks the folder all info will go.. all are important information @MikeBrant – VPR Feb 20 '15 at 04:11
  • In most cases one should only store file path references to where the file exists on the server in the DB. – Mike Brant Feb 20 '15 at 04:19
  • Since this question just points to another in a coding forum, I don't think it is on topic. We ask that questions are self-contained here. Voting to close. – halfer Oct 28 '15 at 19:53

1 Answers1

3

$this->input->post('photo') in model won't work to retrieve the image information. Because the images are stored in $_FILES not in $_POST. So you need to use the upload library in codeignitor like below.

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);
   } 
Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66
  • Mr.Mahendran i am getting issue in $imgdata['full_path'])....full path means please share the example of that –  Feb 07 '16 at 15:26