Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing images in DB vs in Folder Structure

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?

like image 357
bdhar Avatar asked Mar 03 '10 12:03

bdhar


1 Answers

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.

like image 87
Rune Avatar answered Oct 14 '22 05:10

Rune