Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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 6 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. – Marc B Jun 6 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 6 at 16:06
@MarcB: Could work if you used the Data URI schema. – Orbling Jun 6 at 16:10

1 Answer

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;?>">
share|improve this answer
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. – Sheeshkamal Vishwakarma Jun 6 at 16:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.