Store Images in MySQL Database with PHP

  • Avinash
  • 10
  • Jan 18, 2012
  • Web Development

For any web application uploading an image is comman task. But what is the comman way to manage the uploaded files?

Generally what we do is place the uploaded file on specific folder and store the name of the image or some times path to image in VARCHAR field of the database table.

But this article is to cover the concept to store the image in mysql database. Later on we will discuss how to show the stored image from database to the web page.

For this article I am assuming that you are somewhat familiar with the PHP basic file upload process. So without explaining that process I will directly go with the main part of this article.

I will directly show you the code which insert the image into MySQL database. But before moving to that code let me explain the datatype which we are going to use for the image. We will use BLOB datatype of MySQL.

BLOB in MySQL

BLOB stands for Binary Large Object. This types used to store the Large Binary Objects. We can have 4 type of this Datatype.

  1. TINYBLOB
  2. BLOB
  3. MEDIUMBLOB
  4. LONGBLOB

Store Image in MySQL Database

Now considering you have pressed the upload button for the image and you are at code block at which you want to save image in the database table. Have a look at the code block for the same.

// Check if we have file uploaded or not. if(is_uploaded_file($_FILES["to_upload"]["tmp_name"])) { // Instead of moving file to any specific location // We will read the binary content of the uploaded image $image_content = file_get_contents($_FILES["to_upload"]["tmp_name"]); $image_content = base64_encode($image_content); // $image_content can be stored in the MySQL table with BLOB datatype }
  // Check if we have file uploaded or not.
if(is_uploaded_file($_FILES["to_upload"]["tmp_name"]))
{
// Instead of moving file to any specific location
// We will read the binary content of the uploaded image
   
$image_content = file_get_contents($_FILES["to_upload"]["tmp_name"]);
$image_content = base64_encode($image_content);
   
// $image_content can be stored in the MySQL table with BLOB datatype
}

This $image_content variable can be stored in MySQL table in the fields with datatype BLOB. In next post we will see how to show this stored binary data as an image in webpage.

Subscribe to RSS Feed to keep updated with the new upcoming posts.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Lumbendil

    You’re explaining how to store them in the DataBase… but why would one want to do that in the first place? An image is quite large, so the connection handling this is going to need more bandwith to deliver you the image.

    The benefits, on the other hand, aren’t clear. If you store the path, the only downside from what I see is that when you want to backup your website data you have to remember to make a copy of the uploaded images too.

    Apart from that, wouldn’t BLOB types handle correctly the first input, the output of “file_get_contents”? Why is it needed for the content to be encoded in base64? It’s an extra operation and increases the size of the image.

  • Armand

    Won’t this significally slow down your website if you have a large database of images?

  • http://www.xpertdeveloper.com Avinash

    There are not any major benefits over other methods but i this is the alternative way. I strictly have to follow this methods as per client’s request. :)

  • Pingback: Store Images in MySQL Database with PHP | Database | Syngu

  • Pingback: View Image Stored in Database on Webpage | Expert PHP Developer

  • Adrian

    In my opinion, this is a bad solution. This will increase your data-storage, slowdown your application and under some circumstances you will get problems by creating a backup (to large db).
    If you work with a MASTER/SLAVE config… forget it!

  • http://coolsoft krish

    can u tell me code for save file path in database instead of directly save the file in to database while upload a file?why means when u save directly it occupie large memory in database and also when take dump of database u want import u r not able to import dude to high memory of dump . so i want code for
    how o save path of uploaded content

  • Amanosa

    Thank you but i think i will be needing alot of help out here…regarding the viewing of the image..

  • Bartek

    @Lumbendil, this is sometimes required for maintaining transactional coherence. Say a process deletes a record from the database but there is a system failure before the actual file is removed. You end up with more files than what you have in the database. This in turn causes filename conflicts if those are generated by your app based on what’s in the db. A reverse situation is no less scary – you start serving non-existent files. Of course, all this can be handled but it takes additional effort.

    Now, for large systems there are obviously dedicated, enterprise-grade solutions available with various kinds of bells and whistles. The thing is these are either expensive or require major setup effort. With low-traffic or low-support apps moving everything onto the db instead of coding workarounds might not be such a bad idea. If the images are sane size-wise you know you won’t suffocate the caches, the performance impact is negligible (even 200% of 0.2s is unnoticeable to the end-user) while having everything in one place, where db itself handles concurrency, transactional safety, and backups can be a major relief.