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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

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

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

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

Post by mattpointblank »

I agree, storing them on the filesystem is much better. For one, it makes moving/backing up databases much smaller and easier.
Post Reply