Loading Images from MySQL db to view in CI

Hey guys,

My current problem is loading images from MySQL database to the view in CI.


    $val = "1";

    $con = mysql_connect("localhost","root","bla");
    if (!$con)
        die('Could not connect: ' . mysql_error());
    mysql_select_db("studentDB", $con);

    $val = 1;
    $dbQuery = "SELECT photo FROM students WHERE stud_id=$val";

    $result = mysql_query($dbQuery) or die("Couldn't get file list". mysql_error());

    if(mysql_num_rows($result) == 1)
        $photo = mysql_result($result, 0, "photo");
        Header("Content-type: image/jpeg");
        echo $photo;
        echo "Record doesn't exist.";


The above is the code I used in a PHP file (outside CI) for testing purposes, which successfully loaded the image from the database.

I pasted the exact same code on a view in my CI folder(which I figure should work), and ran it through a controller function which redirected to this view and what came out was gibberish instead of the image.

The 'photo' field type is of 'blob' format.

I wonder if there is any extra lines of code I missed out.
Thanks in advance!

can you please post your controller here so that we can have a look what you were missing out

Thanks for the reply.

Note, the above code is just merely to test the loading of images from the database. Thus the controller function, is merely loading the above view using $this->load-view(bla) statement.

Just as an update, I also have tried using CI's db helper to run queries on the model, thus retrieving the necessary values from the view. The result was similar to the one faced above. Other values from the db table loaded properly. The only problem is the image. Ive tried different 'content-types', such as 'jpg, blob..', but it still cannot work..

Did you ever get to know how to resolve this? Same thing here, lost already 3 days and gained a lot of gray hairs. thanks !


I just spent a few minutes testing that ... no problem at all.

CREATE TABLE ci_images (
  id int(11) NOT NULL,
  image blob NOT NULL

model (image_model.php):
public function get()
        $query = $this->db->select('image')
                            ->where('id', 1)
        $result = $query->row();
         return $query->num_rows() === 1 ? $result : false;                

controller (image.php):
function show()
        $item = $this->image_model->get();
        Header("Content-type: image/jpeg");
        echo $item->image;

When I load a jpg file into my blob, I can see it in my browser typing http://localhost/image/show

Hopefully this code will work for you ...

unfortunately not, I'm not using localhost. When using your script, the image (of 81kb) is truncated to 64, the only output is a string: "http://www.cardpresso.com/index.php/sitemanager/show"

Note that a MySQL BLOB can only hold 2^16 bytes (64K)

I have tested with a 81k image in a LONGBLOB ... works fine

the writing is not the problem, I've the file in a longblob and the file data in a seperate table. the problem is that the file read only WORKS with the script OUTSIDE the CI framework. The exactly same script doesn't work when placed as standalone CI contoller or as function within a CI contoller. It doesn't show the image or says the zip file is corrupt. I suspect I'm loosing a byte or 2 somewhere, maybe because of an 'integrated' CI escape ? or similar ?

any ideas?

[eluser]Victor Michnowicz[/eluser]
Some things to try:

- Try a lowercase "H" in your headers() function. "headers", not "Headers"
- Check your database character set and collation. CI defaults to a UTF8 character set and a UTF8 general CI collation. If your database is setup differently that may produce a problem.
- Try a different host. If you have a cheap host, they may not like image URLs that do not look like image URLs. Some .htaccess trickery may be in order. Possibly removing "index.php" could help.

[quote author="vickel" date="1292292154"]unfortunately not, I'm not using localhost. When using your script, the image (of 81kb) is truncated to 64, the only output is a string: "http://www.cardpresso.com/index.php/sitemanager/show"[/quote]

1/ Is your problem occurring whatever the size of the image ?
2/ When you say truncated to 64, do you mean 64 bytes or 64k ?
3/ It could be useful to trace the size of your image between the database read and the echo command and compare it to the initial size

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.