![]() |
Reading an image from a database.. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Reading an image from a database.. (/showthread.php?tid=22928) |
Reading an image from a database.. - El Forum - 09-23-2009 [eluser]Unknown[/eluser] I was already successful in inserting an image file to a database using the following code: Code: //Controller When I tried to retrieve the image, using this code: Code: //View ..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. Reading an image from a database.. - El Forum - 09-23-2009 [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'); 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'); 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); 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 ![]() Reading an image from a database.. - El Forum - 09-24-2009 [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! |