views:

156

answers:

2

What are the advantages and disadvantages of storing an image as a blob in the database vs storing just the file name in the database.

I'm using PHP(CodeIgniter) with MySQL.

I know this question is subjective but a client asked me this question and I couldn't give a good answer.

+10  A: 

I'd generally say that :

  • database is harder to scale :
    • having a DB that has a size of several GB because of images will make many things (backup, for example) harder.
    • do you want to put more load on your DB servers, to serve... files ?
  • serving images from the filesystem is something that webservers do well
    • and it allows one to do some tasks (like generating thumbnails, for example) without involving the DB server
    • I suppose, if needed, that using plain old files will also make things easier the day you want to use some kind of CDN

Which means I would almost always store images / files in the filesystem, and only store the file's name (and, possibly, size and content-type -- that can always be useful) in the database.


Thinking about it, that question is probably a duplicate... Oh, yes, it actually is ; see at least those questions+answers :

Pascal MARTIN
+1  A: 

I doubt your client have enough experience to judge an answer.
I was asked once by customer to store image strictly into database. After some investigation I've discovered that they just meant "to store images on the server side", because they had an MS Access database before and it has a choice between storing images in the database or just links to the images, while image itself stayed at the users hard disk and often been unavailable.

As for your question, I doubt that image has any relation with relational model. You cannot sort, filter, relate with other rows based on blob content. While you have to do many things to support images in the database - dedicated script to show, emulate conditional get, etc. I know, this feature exists. But it's more an obvious attempt to follow the fashion than a real need, especially in the web development, where images being requested with separate requests.

Col. Shrapnel