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 Revisited

Filesystem or Database?

First let's look at your options when you want to save uploaded images. The two main options are to store file data in your database or to store it on the filesystem.

Typically, if you're going to store the files on the filesystem, you are going to have a database table to store extra information about the file (such as a description for the file, who uploaded it, and so on). So in this case we're simply talking about storing the actual file data.

I'll let you make your own decision on what's best for you, but here are some of the advantages of both methods.

Advantages of Database Over Filesystem

  • All file data is stored in a single location (not on both filesystem and database). If you want to back up your images you only need to do this in a single location.
  • Using a filesystem you may lose a file then have a stale database record pointing to it. The opposite may also happen (have a file without corresponding record).

Advantages of Filesystem Over Database

  • Some file operations may perform faster since these are low-level operating system functions
  • May be easier to replicate files onto other servers (such as Content Delivery Network) using common tools such as rsync.
  • Your database will not be bloated by extremely large files
  • Reading large files from the database may slow down other connections to the database

In This Article



Bonus listings: 1 available