Database image storage versus disk based storage

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
denz
Forum Newbie
Posts: 18
Joined: Fri Jun 24, 2005 4:18 pm

Database image storage versus disk based storage

Post by denz »

Hi All,

I'm currently designing a website which I expect to grow quite large. This website will contain a lot of news stories which will contain many images, plus image galleries. I thought that storing all of the images in a MySQL database would be an efficient way to manage the images. Then I considered that perhaps storing in a datbase could not be the most efficient method of storage in terms of speed.

So, my question is this - What is the *prefered* method of image storage for you guys, can you point me in the right direction on this topic. Prehaps on this forum (I've tried searching but there are too many results! :oops: ), or elsewhere on the 'net?

I'm sure there are, and have been, many discussions about this and I would be very interested in finding others views on this subject.

Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Imho there is only one specialized system for file storage: the filesystem.

Usually, you choose for an in-between. Store the files in the filesystem and then store the path somewhere in your dbms...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

timvw's post++

There are only two reasons I can think of for putting the image binary/blob into the database and they are both very rare.
1) You can lock down access to the database better then the filesystem in some shared hosting environments and/or you have a bigger quota in the DB than on the machine.

2) You have some amazingly stored procedures/custom datatypes in the DB that let you know interesting image-based matching, etc
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Note that if you have your MySQL databases on their own servers, and they actually have to talk to your HTTP servers on another machine, transferring all that information's gonna take up a lot of bandwidth on whatever connection you've got the two hooked up by.
stukov
Forum Commoner
Posts: 26
Joined: Sun Jul 24, 2005 2:16 pm
Location: Sherbrooke, Qc, Canada

Re: Database image storage versus disk based storage

Post by stukov »

denz wrote:What is the *prefered* method of image storage for you guys, can you point me in the right direction on this topic. Prehaps on this forum (I've tried searching but there are too many results! :oops: ), or elsewhere on the 'net?
I saw many people doing:
Small size images -> database
Others -> filesystem

But, as timvw said, storing only the path to the file in the database would be an efficient way - well, this is what I do and it works pretty good.
denz
Forum Newbie
Posts: 18
Joined: Fri Jun 24, 2005 4:18 pm

Post by denz »

great! thanks for your help guys
npeelman
Forum Commoner
Posts: 32
Joined: Tue Jul 27, 2004 5:13 am
Location: Oviedo,FL.

Re: Database image storage versus disk based storage

Post by npeelman »

denz wrote:Hi All,

I'm currently designing a website which I expect to grow quite large. This website will contain a lot of news stories which will contain many images, plus image galleries. I thought that storing all of the images in a MySQL database would be an efficient way to manage the images. Then I considered that perhaps storing in a datbase could not be the most efficient method of storage in terms of speed.

So, my question is this - What is the *prefered* method of image storage for you guys, can you point me in the right direction on this topic. Prehaps on this forum (I've tried searching but there are too many results! :oops: ), or elsewhere on the 'net?

I'm sure there are, and have been, many discussions about this and I would be very interested in finding others views on this subject.

Thanks
Define 'Quite large'? Yes, as the others have posted you can store the path to the images in th DB and call by the URL but, if you are going to be storing thousands upon thousands of images then you may run into some filesystem restraints. You can lose time (efficiency) by having too many files in one directory. You would have to split (catagorize) them somehow.
If you store the images in the database then you need a simple script to output them - no big deal. And one big advantage to keeping them in the DB is backups... you don't have to worry about your news articles and images being out of sync. When you backup the DB, everything is backed-up, text, images, everything.

Norm
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Storing images in a db would feel like keeping my shoes in the fridge.
Bitmaster
Forum Newbie
Posts: 20
Joined: Thu Nov 21, 2002 8:42 am

Post by Bitmaster »

...Storing images in a db would feel like keeping my shoes in the fridge...

Well here's an idea: some web hosting companies don't allow the user apache is running as (usually "nobody" or "apache") to have write access to the filesystem, so you use a database or you don't write anything to the disk...

I know this because I ran into the same problem with a customer recently :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

In that case i would suggest you to buy a shoebox instead of a refrigerator for storing shoes (Read: there are more than enough webhosters out there, get one who knows what he's doing :p)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you're having trouble writing files with php chmod the parent folder 0777 *temporarily*. Now you can make a dir with php so when you chmod the parent folder back to something safer - like 0755 - the dir you made can be written to by php scripts.

Same thing with files. Once you have created a file with php, you can continue to write to it after the non-php-owned parent dir has been chmod'd back to 0755.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Interestingly, once I CHMODded something to 0777, and it didn't work, and when I CHMODded it to 0755, it did work... so 777 is the solution to everything, but it's worth a try.
User avatar
vd
Forum Newbie
Posts: 6
Joined: Tue Aug 30, 2005 3:43 am
Location: Luxembourg

Post by vd »

storing your images in the database will cause problems uploading a dump e.g with phpMyAdmin, when "upload_max_filesize" and "post_max_size" were set to a lower value than the total filesize.
Post Reply