Storing uploaded images - Database or folder?

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
senthil
Forum Newbie
Posts: 12
Joined: Wed Jan 15, 2003 6:42 pm

Storing uploaded images - Database or folder?

Post by senthil »

Hi,

I want to allow uploading images in my website. I would like to know which is a better way to store the images. Is it better to store images in the database as a binary file or is it better to store the images in a folder and store a reference to it in the database? Or is there any other better way to store the images. If anyone can tell me the pros and cons of doing it either way, it will be very helpful.

Thanks
Senthil
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Post by Skywalker »

It's better to stor images in to a folder and stor the path in the database.
;)
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

Skywalker wrote:It's better to stor images in to a folder and stor the path in the database.
;)
As pros and cons you can't beat that LOL :D

I prefer to use database to store the image for a few reasons.

1) you don't have to play with the folder's and file's permissions all you have to deal with is the database grant and you're OK.

2) when you manage those images it's easier to delete a bunch of images from a specific users with a database then on folders.

3) it's easier to defrag a database then a server HD... When you delete a few images you simply send an OPTIMIZE() command to your MySQL (MYASAM) table.

4) potentially a more secured way to display images because you don't have any other choice then using a PHP script to display the images... In that script there's nothing stoping you from placing a piece of code to check where the request is coming from and display the image only if the request comes from your server...

5) some hosting company don't count MySQL databases in the total HD space for their accounts. It's not "fair" but that's the way things are. Fortunately for my own ethical wellbeing my current host does count MySQL database in the total space I can use...

On the CONS side

1) if you have a lot of images it can slow your database while the same isn't true for files stored in folders, I'm not exactly sure at what point the performance degradation starts to show but so far I have a few hundred photos stored in my DB and it's still as fast as in the beginning.

2) data lost by a hasty command is a lot harder to cover then on the file system, unless you have a backup of your database before you manage the images your data is vulnerable to a wrong command (you didn't read your admin page correctly and pushed the wrong button...) On the file system you can always recover the files in a few ways (I'm not sure how to recover files so don't ask).

There are probably other PROS and CONS so if you have some I'd like to read them.
senthil
Forum Newbie
Posts: 12
Joined: Wed Jan 15, 2003 6:42 pm

Post by senthil »

Hi,

Thanks very much for ur replies. But after reading the replies, i'm more confused. :cry:
Puckeye, I will let u know if I come across anything useful.

Thanks
Senthil
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Personally I find for simplicities sake that storing images in a folder and just referencing them in the database is easy when it comes to maintaining the images.

Why do you want to store images, maybe if we have a better idea of what you're trying to achieve we can offer more specific advice.

Mac
senthil
Forum Newbie
Posts: 12
Joined: Wed Jan 15, 2003 6:42 pm

Post by senthil »

Hi,

I want the users to upload their items, whatever they have for sale. So, I need to store those images.

Right now, I'm storing in a database. I tried the program and it worked fine.

Thanks
Senthil
Post Reply