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
Storing uploaded images - Database or folder?
Moderator: General Moderators
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
As pros and cons you can't beat that LOLSkywalker wrote:It's better to stor images in to a folder and stor the path in the database.
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK