Store Images in MySQL
May 29th, 2008
Sometimes, it’s more convenient to save images in a database than as files. MySQL and PHP make it very easy to do this. I will describe how to save images in a MySQL database.
- Setting up the database
- Setting the upload script
- Size does matter
- Conclusion
The difference between any regular text or integer fields and a field that needs to save an image is the amount of data that is needed to be held in the field. MySQL uses special fields to hold large amounts of data. These fields are known as blobs (Blob).
Here is the BLOB definition from the MySQL site:
A BLOB is a binary large object that can hold a variable amount of data. The four
BLOBtypesTINYBLOB,BLOB,MEDIUMBLOBand LONGBLOB differ only in the maximum length of the values they can hold.
For more information about MySQL Blobs check out: Reference: Blob
Use the next syntax to create a basic table that will hold the images:
CREATE TABLE Images ( PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY, Image BLOB );
What we need now is the PHP script that will get the file and insert it into the database. The next script does just that. In the script, I’m assuming that the name of the file field is “Picture”.
<? If($Picture != "none") { $PSize = filesize($Picture); $mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize)); mysql_connect($host,$username,$password) or die("Unable to connect to server"); @mysql_select_db($db) or die("An error occured when selecting database"); mysql_query("INSERT INTO Images (Image) VALUES ('$mysqlPicture')") or die("Query failed"); } else { echo "Failure during upload."; } ?>
This is all that is needed to enter the image into the database. Note that in some cases you might get an error when you try to insert the image into the database. In such a case you should check the maximum packet size allowed by your MySQL version. It might be too small and you will see an error about this in the MySQL error log.
What we did in the above file is:
1. Check if a file is chosen.
2. addslashes() to the picture stream to avoide errors in MySQL.
3. Connect to MySQL, choose the database and insert the image.
The theoretical limit in MySQL 4.0 is 2G, however each blob requires generally to have 3 copies of it in the memory (stored in various buffers) so you need a lot of memory, if you have large BLOBs stored in MySQL. This is the reason, why the theoretical limit can be reached only on 64bit systems. The Practical limits are around some hundreds of megs per BLOB.
While logged into MySQL, we should modify the max_allowed_packet system variable. This variable determines how large of a packet (i.e. a single row) can be sent to the MySQL server. By default, the server will only accept a maximum size of 1 meg from our client application. If you do not intend to exceed 1 meg, this should be fine. If you do intend to exceed 1 meg in your file transfers, this number has to be increased.
It’s often suggested to save path to a file in database and not the file itself. Rethink everything before storing large data in your database - it can save lots of memory.
On the other hand this is extremely useful for downloading files of different types from a database, or even displaying images stored in Blob fields.






