BLOB is a kind of MySQL datatype referred as Binary Large Objects. As its name, it is used to store huge volume of data as binary strings as similar as MYSQL BINARY and VARBINARY types.


Classification of MySQL BLOB

MySQL BLOB Types Maximum Storage Length (in bytes)
TINYBLOB ((2^8)-1)
BLOB ((2^16)-1)
MEDIUMBLOB ((2^24)-1)
LONGBLOB ((2^32)-1)

In this MySQL tutorial lets us learn to insert and read MySQL BLOB using PHP. To start with, we need to create a MySQL table with a BLOB column and the SQL script is,


CREATE TABLE IF NOT EXISTS `output_images` (
  `imageId` tinyint(3) NOT NULL AUTO_INCREMENT,
  `imageType` varchar(25) NOT NULL DEFAULT '',
  `imageData` mediumblob NOT NULL,
  PRIMARY KEY (`imageId`)

Insert Image as MySQL BLOB

To insert an image into MySQL BLOB column the steps are,

  1. Upload image file.
  2. Get image properties (image data, image type and more.)
  3. Insert image file into BLOB.

PHP script to insert BLOB data is,


if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("phppot_examples");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />

After executing this script image upload form will be shown,


On form submit, this PHP code gets the content of the image file and stores it into the MySQL BLOB column as binary data.

Read Image BLOB to Display

For displaying BLOB images to the browser, we have to create a PHP file to do to following.

  • get image data and type from MySQL BLOB
  • Set the content-type as image (image/jpg, image/gif, …) using PHP header().
  • print image content.


$conn = mysql_connect("localhost", "root", "");
mysql_select_db("phppot_examples") or die(mysql_error());
if(isset($_GET['image_id'])) {
$sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["imageData"];

This PHP code will display MySQL image BLOB data. From HTML image tag we can refer this PHP file with the corresponding image_id as an argument. For example,

<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" />


$conn = mysql_connect("localhost", "root", "");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
$result = mysql_query($sql);
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
while($row = mysql_fetch_array($result)) {
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>




Download MySQL BLOB using PHP Source Code

This PHP code tutorial was published on December 30, 2013.

↑ Back to Top