Should I store an image file directly in the database or store it's url in the database?
Apr 15th, 2008 07:54
Taksh Verdhan, Cortez Cortez, Tony Hughes, Subbu S,
There are a few considerations for this question. I'm assuming you have
a file upload system and are storing the images locally on the server.
If you store just the URL to the image in the database, then all that
the server needs to process is that text field. The client (i.e. the
browser) then retrieves the image at that location. You need to be
particularly careful that the users don't upload non-image files such as
viruses or scripts, that the permissions of the upload folder are set
properly, and so on. This method is easy on the server. If the images
are not stored locally then you will possibly have missing images.
The other way is to store the whole image in the database, and if the
images are large, or there are a lot of them, you end up having a
massive database. This can be a problem if you're using something like
Access. However, if your database solution can handle it (I'm thinking
Oracle, SQL server, MySQL) then this can actually be an advantage. If
the database is loaded into RAM, then images which are frequently
accessed can be served quickly instead of going to disk (if you have a
good server setup this will happen regardless). Also, you have more
control over how the images are displayed because the image is produced
as part of a query attached to the larger page. For example, each image
record could have virtually unlimited fields attached to it, like which
users or user groups can view it. You can store this information in a
database anyway but if the actual image is with that data, it really
hands you more control over the image.
Remember that in database form you can't easily copy the images from one
folder to another, with FTP or something like that. A file system is
really just another database, and by putting the files in your own
database you're limiting yourself to your own tools.