0

I am trying to store image in mysql using codeigniter. Data is getting successfully inserted but i am not able to view that image.

enter code here
$this->gallery_path = realpath(APPPATH . '../img');
    $config=array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' =>2000
    );
    $this->load->library('upload',$config);

    $this->upload->do_upload('business_icon');

    $upload_data = $this->upload->data();

        $data_ary = array(
            'title'     => $upload_data['client_name'],
            'file'      => $upload_data['file_name'],
            'width'     => $upload_data['image_width'],
            'height'    => $upload_data['image_height'],
            'type'      => $upload_data['image_type'],
            'size'      => $upload_data['file_size'],
            'date'      => time(),
        );

        $image_data=fopen($this->gallery_path.'/'.$upload_data['file_name'],'rb');
        $image_data1= fread($image_data,$upload_data['file_size']);

        $image_type='image/'.$upload_data['image_type'];



    $insert_data=array(
        'business_name'=>$this->input->post('business_name'),
        'business_icon'=>$image_data1,
        'icon_type'=>$image_type,
    );  

    $this->db->insert('businesses',$insert_data);
    return $this->db->insert_id();

Can any one help me on this how can i store image in db and also read it in codeigniter.

  • What is the problem with viewing it? You can start by sharing the code you have for the controller/action that reads the data from database and outputs it to the browser. – hw. Jun 06 '13 at 15:51
  • 1
    showing the insertion code is rather pointless if that's working. you need to show the code that's retrieving/displaying the image. probably you're doing `` which is utterly incorrect. – Marc B Jun 06 '13 at 15:54
  • Usually better in most cases to store images to disk and just store a reference to their existence/location if needed in the DB. – Orbling Jun 06 '13 at 16:06
  • @MarcB: Could work if you used the [Data URI schema](http://stackoverflow.com/questions/6819314/why-use-data-uri-scheme). – Orbling Jun 06 '13 at 16:10

1 Answers1

1

a) make sure type of field business_icon is longblob in mysql.

b) make sure to output the correct header (Content-Type) and data when reading it, DO NOT use something like:

<img src="<?php echo $data;?>">
tess3ract
  • 183
  • 7