Storing images in DB vs in Folder Structure

bdhar picture bdhar · Mar 3, 2010 · Viewed 8k times · Source

I understand it is possible to store images in Databases as Binary large objects. But I used to see in some forum web applications that they are stored as flat files in web server machine and retrieved when needed.

What is the advantage and disadvantage in both methods?

When to go for which approach?

Answer

Rune picture Rune · Mar 3, 2010

As usual, it depends. You need to consider the usage pattern of the images and what features your DBMS provides.

Storing images in the database:

PROS

  • If the images are to be associated with entities in your database (say, a user), the database can take care of maintaining that relationship. If, on the other hand, images aren't associated to anything in the database, you will probably not want to store them in the database.
  • If your database supports it, you will be able to process files within a transaction (I believe MS SQL 2008 supports this, I don't know if others do).
  • If you need to store multiple versions of each image (say, because they change over time), it will probably be easier to do in the database than on the file system.

CONS

  • You will be putting a lot of strain on the database.
  • Backing up your database may take a long time.

Storing images on disk:

PROS

  • Making backups is trivial
  • Inspecting images etc. just requires a file browser, no need for a database client

CONS

  • Keeping the database's view of the image collection and the actual content on the disk in sync may be non-trivial, depending on the operations you will be performing on the images.

Of course, all these concerns are particularly valid if you store large numbers of images.