PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Storing Images In MySQL

Note: This article has been replaced by Storing Images in MySQL Revisited (by Quentin Zervaas, 2010). We've left this article here for historical purposes, but we strongly recommend you use the newer article instead.

Isn't This A Bad Idea?

This argument continues to rage and many lives have been lost trying to prove that this is a Bad Idea™. The facts are, this method of binary storage is very successful and has many advantages over filesystem storage. Some advantages are:

  • Referential integrity
  • Ease of backup
  • Saving of Inodes
  • Easy categorisation
  • Central point of operations

Many of the “nay sayers” will have you believe that storing of images or other binary data creates too much overhead. A large database can be stored on a RAW partition and in fact be faster as it has no filesystem overhead to contend with.

Some would say that access is slow when SELECTing from the database, but most of these issues are caused by coders doing things like:

Listing 1 listing-1.sql
select * from image_table

When all they needed was the image ID. You can see a benchmark of the perfomance of this where we used three methods of access for an image:

  • Access image from file
  • Store image in filesystem and store the url in the database
  • Store the image in the database

The results may surprise you.

In This Article


Article History

Apr 16, 9804
Initial article version
Jan 18, 2008
Updated listing 5 to not include non-existent conf.php