In this tutorial i will explain you that how we can upload our image to MySQL Database using PHP. In this tut i am also introduce you with BLOB column type in MySQL.

What is BLOB?

A blob is a collection of binary data stored as a single entity in a database management system. Blobs are typically images, audio or other multimedia objects, though sometimes binary executable code is stored as a blob. Wikipedia
In Simple words BLOB is used to store data to database in a binary format.
MySQL have four type of BLOB :

  • BLOB

BLOB help us to solve these issue

  1. Store image to database give us better security.
  2. Eliminates messy directory and file structures.

But there are some disadvantages of BLOB like

  1. You are unable to access your file directly using ftp
  2. It will increase your database size.
  3. Migrating to a new database is made more difficult.

Now we create a simple form to upload and display image from database using php.
Database :

Code Form :

simple form where we select file to upload.

Database Connection conn.php

Handle form data and upload image to database

In above code we simply get uploaded image and insert it to database. You also add your validation in above code.

Now we display uploaded image:

In above code we display our uploaded image on browser using base64 encoded picture.

A simple code but good to learn.

Posted in: php