According to survey by SitePoint, there are about 67% of developers never store image in the database, and only 1.4% saying they always do. So what is the difference between two methods of storing images? What are the pros and cons of each
methods?
I have been working as PHP programmer for just more than a
year now, and just recently I encounter the issue of store images in the database. I have never done this before, so I googled this issue and found the following opinions by developers:
Reasons not to store images in
the database:
[ul]
[li]When you actually want to use the images, getting the images from the database is inefficient – since the image usually is sent to the client one by one. Imagine having, for instance, an web based image gallery. If the images are stored as files, displaying 20 images is as simple as including img src=”path_to_image” 20 times in your html page, and the browser will request the images from the web server. Having the images in a database requires some logic to select the binary data, and sending it to the browser. This probably means having a script (php?) doing this for you, and your html file will include 20 img src=”imagehandler.php?imageid=xxx”. This will result in at least 20 extra queries…[/li]
[li]Manipulating images are far harder. With text, most databases can update the strings directly via an SQL update/insert command (update stringcol set str=’habba’ where str=’dabba’). If you, for instance, want to resize all your images, you have to create some logic to export the images, resize them, and import them back into the base.[/li]
[li]If you have huge amounts of images, doing incremental backups of the filesystem is usually more practical (in regards to storage needs, speed and so on) than doing full backups of the database.[/li]
[/ul]
Reasons to store the images in the
database:
[ul]
[li]Everything is in one place.[/li]
[li]Nobody will accidentally delete a file and forget to update the path in the database[/li]
[li]Nobody will accidentally put a new file in the filesystem and forget to update the database[/li]
[li]Easier to export database across different development environment[/li]
[/ul]
I personally won’t store images in the database, mainly for the speed and database management issues. I feel it looks ugly to store huge binary data in a database. Anyway, just my opinion.