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


This article shows you how you can store images in MySQL using the Blob column type. I will show you to create a file upload form, then store the uploaded file in the database. The concepts covered in this article can apply to all files in general, not just images. This is an updated version of a similar article that already appears on PhpRiot.

To begin, I'll discuss the merits of storing files in a database table compared to storing files on the filesystem. While I'm not advocating you use one method over the other, in this article we will only be implementing a solution that stores image data in a MySQL database table.

Next, I'll introduce you to the blob data type and show you how to create a MySQL database table to store image data. I'll show you how to create an HTML upload form and to store uploaded images in the database.

Once there are images in your database you can output them on your site. I'll show you how to list all images in the database, as well as how to display the images.

This article covers interacting with MySQL, creating upload forms and handling upload data, looping over database data and sending custom HTTP headers.

Additionally, there is a bonus listing included with this article will show you how to generate thumbnail versions of your uploaded images.

In This Article

Bonus listings: 1 available