Welcome Guest, Not a member yet? Register   Sign In
Reading an image from a database..
#1

[eluser]Unknown[/eluser]
I was already successful in inserting an image file to a database using the following code:

Code:
//Controller
$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);
        
if (!$this->upload->do_upload()){
    echo 'error uploading file!';
}else{
    echo 'success uploading file!';
    
    //Insert file to database.
    $file_info = $this->upload->data();
    $file_name = $file_info['full_path'];                
    $image_file = addslashes(fread(fopen($file_name, "r"), filesize($file_name)));    
    $this->dbc->img_to_db($image_file);
}

//Model
//I only have two fields in my images table, id and image
function img_to_db($image_file){
    $sql = "INSERT INTO images (image) VALUES (?)";
    $query = $this->db->query($sql, array($image_file));
}

When I tried to retrieve the image, using this code:

Code:
//View
<img src="&lt;? echo base_url(); ?&gt;view_img" />

//View_img Controller
function index(){
    header("Content-type: image/jpeg"); //I tried image/jpg, still didn't work.
    $image_file = $this->dbc->get_img_from_db(1); //Fixed value for now, just to test it
    echo $image_file->image; //Print or echo?
}

//Model
function get_img_from_db($image_id = 1){
    $sql = "SELECT image FROM images WHERE id = ?";
    $query = $this->db->query($sql, array($image_id));
    return $query->row();
}

..all I get is a broken image icon. Is there something wrong with what I am doing? If I removed the call to the header function in the controller, I was able to see the weird characters, which I assume is the image in a format that can be stored in the database.
#2

[eluser]BrianDHall[/eluser]
http://us.php.net/function.getimagesize will give you the supposed mime type of the file before you store it in the database, so you'll probably want to save that.

Check to make sure your mime is correct, because so long as the file info is stored correctly it should work.

Though I was noticing you did use fread, fopen, and addslashes - the addslashes makes me wonder if you aren't corrupting the image data, but I don't usually do it that way so I don't know.

Here's how I do it after using the upload class to upload a file:

Code:
$this->load->helper('file');

$getinfo = getimagesize($data['full_path']);

$img = read_file($data['full_path']);

Then I just add the image data to the database basically with ActiveRecord and let it handle any escaping that may be needed. I use ORM (DMZ) so my code is actually:

Code:
$this->load->helper('file');

$getinfo = getimagesize($data['full_path']);

$img = new Db_image();
$img->data = read_file($data['full_path']);
$img->save();

But the logic remains the same. I then save the mime type of the image along with the image data, then do a simple:

Code:
header("Content-type: " . $details->mime);

echo $img->data;

I can tell you I was running into problems with transparent PNGs...so I just exluded that because I didn't care enough to look into why Big Grin
#3

[eluser]Unknown[/eluser]
Never mind. I read some problems with storing images in a database, they said it'll be better off if I store the path to the image and just save the image in a folder somewhere. Thanks anyway!




Theme © iAndrew 2016 - Forum software by © MyBB