Page 1 of 1

Is It a good Idea to store Images in a Database?

Posted: Sun Jan 25, 2009 6:22 am
by tinoda
I am trying to develop an image gallery with categories using php and intend to store these images in an ODBC database. Since I will be having many categories is it advisable to use a database. I am thinking images might slow down the database, etc.

Re: Is It a good Idea to store Images in a Database?

Posted: Sun Jan 25, 2009 4:55 pm
by Stryks
I cant give you any real hard reason why not to store all your images in a database, but I can tell you it's not the way I would do it.

I'd store the images on the filesystem, and have a database entry for each image stores containing the filename and whatever other related information you want to associate with each image. To pull a file, you get the row for the image you want and display the filename associated with it.

If the idea is to store images in the database is to secure the images, then you can put the images in a non-world visible directory and use a php script to access each image by it's database ID. This way you can check to make sure that the viewer is allowed to see and image before it is displayed, and of course, images can't be viewed without the script.

So for example, the URL to display an image would be 'viewimage.php?id=736352' instead of 'image1234.jpg'.

The only other thing I can think of in regards to database storage will depend on the db engine itself, but there are a few db's that have a maximum size.

Anyhow, hope this helps.

Re: Is It a good Idea to store Images in a Database?

Posted: Sun Jan 25, 2009 5:02 pm
by John Cartwright
The only legitamate reason I've heard to date is to avoid namespace issues between files. Using a database we do not internally reference the files by their filename, so we can have multiple files with the same filename. However, this can also be accomplished with a tiny bit of extra work usng the filesystem to store the files, and the datbase to store only metadata.

Generally, you will lose performance for little to no gain.

Re: Is It a good Idea to store Images in a Database?

Posted: Sun Jan 25, 2009 5:14 pm
by requinix
By storing the images in a database, whenever you want to display one a script has to connect to the database. That means more connections to the database, more strain, and a higher chance of cutting off access to some script that really needs it.

I'll eat my hat if somebody can give a good reason (that can't be addressed another way) as to why you should store images in a database.

Re: Is It a good Idea to store Images in a Database?

Posted: Mon Jan 26, 2009 3:02 am
by mattpointblank
I agree, storing them on the filesystem is much better. For one, it makes moving/backing up databases much smaller and easier.