• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing images in database using active record

Generally speaking, I agree that images (or BLOBs in general) shouldn't be stored in the database... however, I have run into some instances where BLOBs are desireable - granted they weren't web-based apps, but desktop, so performance and demand is less of an issue - where the rules of HIPPA and Sarbanes\Oxley are involved. This is because it restricts access (and keeps the net admins happy as they don't need to maintain ACLs for folders) and even if you could select * on the table you're not going to get anything immediately useful.

That said, I would try to avoid that situation as much as possible, and if I can't, I might go for something beefier like SQL Server or Oracle.

Also, if you ever want to see a DBA cry, tell him you plan to store thousands of images in the database. I'm in the middle of an application changeover where we are removing the images from the DB and storing them on the file server instead. No one likes to see a DBA cry (often).


[eluser]Watermark Studios[/eluser]
Portability and industry restrictions sometimes require DB image storage. I had to do that with a recent Java WebObjects/FrontBase application. Granted, doing this on a high-volume application is going to really task your DB server. Besides, it's so easy to just do file handling using PHP. IMHO, it's better to store the images outside of the public architecture. Using PHP, you can easily grab files from other folders on the system if you have the permissions set properly.

Best of luck,


What about Base64-encoding the image data before storing it and decoding it when you retrieve it?
$book = array(
            'title' => $this->input->post( 'title' ),
            'cover' => base64_encode($cover)
Then your data won't need any escaping. This will increase storage requirements and would presumably decrease performance, but those may not be the most critical considerations if you really need to store images directly in the database.

(In general, I find it disappointing when someone asks how to accomplish something and others only tell them not to do it at all, without having any knowledge about the reasons behind the original inquiry. If it's a bad idea, go ahead and say so, but provide an answer too. Sometimes the bad idea is the lesser of evils.)

I need to store images on DB, because the DB is shared with other projects that retrieves images from the tables. Anyway, I found the solution. Just need to specify the field to be stored in raw format, and not escaped by active record. And this is the way to do this is:

$this->db->set( $book );
$this->db->set( 'cover', "'" . addslashes( $book['cover'] ) . "'", FALSE );
$this->db->insert( $this->tbl_name );

Thank you!!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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