Saving images in database mysql

All we need is an easy explanation of the problem, so here it is.

My client have created a script in php+mysql that saves images directly in the database and every images has url like this:

In your opinion this is a very wrong solution ? Should I rebuild all the site ?

There are about 1000 visits per day and about 600 images in the database.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Images files are files, unless there is compelling reason to store them in the database, the place they belong is the file system, where you get benefits like easy backup and replication, distributed across multiple machines, directly served by the webserver, and all the last-modified-date and etag supported by the webserver to achieve better performance, etc.

if the number of images is expected to grow, and different sizes of one image are expected to be created (e.g. thumbnail images etc.) in the coming release, a plan to migrate images from db to file system is strongly suggested.

Method 2

Is there a good reason for storing them in the DB? There can be some valid reasons to do this. I am assuming these are client images not the images needed to display the site itself (i.e. background images, things inside img tags).

As for rebuilding the site. It would likely improve performance, but at a 1000 hits a day it may not be worth the time it would take. assuming site performance was acceptable and it did not incur extra hosting fees etc. I would be inclined to leave it until I was redesigning the site.

Method 3

If you read @Gazler’s link from comment, it really boils down to few questions:

  • is there any problem with performance? [Y]
  • do you store any additional information, or just images itself? [Just images]
  • do you plan (or hope) to attract much more users in future? [Y]
  • are you limited in size of your db? [Y]

if you answered with answers in brackets, then it might be good for your website to put images out of database, otherwise, you can leave it as it is and save yourself some time and/or nerves

Method 4

Just because it can be done, doesn’t mean it should be.

I always store meta data about the images (filename, location, other attributes) in the database, and store images externally in the file system. There is very little benefit, imo, of actually storing images in an SQL database. Once they are in there, they are very inconvenient to use.

Method 5

There are good and bad points. And it depends on who you are. about 5 years ago it became all the rage to put all images in the database.

But with server space sizes bigger today, its not really about saving space.

A negative would be you will always require understanding of server side programming to use images. If this development my be passed to another person, it could prove problematic.

As far as I can see, if it works, it works and I can’t see performance issues – it seems very quick.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply