Web Development Starts Here…

Store Images in MySQL Database with PHP

Posted by on Jan 18, 2012 in Code Snippet, PHP | 5 comments

Store Images in MySQL Database with PHP

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
}

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.

5 Comments

Join the conversation and post a comment.

  1. 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.

  2. Armand

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

  3. 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. :)

  4. 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!

  5. 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

Trackbacks/Pingbacks

  1. Store Images in MySQL Database with PHP | Database | Syngu - [...] this article is to cover the concept to store the image in mysql database   ...
  2. View Image Stored in Database on Webpage | Expert PHP Developer - [...] by Avinash on Jan 22, 2012 in Code Snippet, PHP | 0 comments In earlier article we have check ...

Leave a Comment