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.

4
  • 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. Commented Jun 6, 2013 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 <img src="$data_from_db"> which is utterly incorrect. Commented Jun 6, 2013 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. Commented Jun 6, 2013 at 16:06
  • @MarcB: Could work if you used the Data URI schema. Commented Jun 6, 2013 at 16:10

1 Answer 1

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;?>">
Sign up to request clarification or add additional context in comments.

1 Comment

is my uploading code correct? Bcoz when i m using simple php with same db i am able to save and view image. But not using codeigniter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.