Upload & Retrieve Blob Image In PHP MySQL


This examples shows how to upload and retrieve blob image in PHP & MySQL

MySQL query for create Table

During the table creation set the image column data type as LONGBLOB.

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image` longblob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Upload Blob Image to MySQL using PHP

<html>
  <body>
    <?php 
    $con=mysqli_connect('localhost','root','','db_sample');
    if(isset($_FILES["file"]))
    {
       $fileName=basename($_FILES["file"]["name"]); //Get File Name 
       $fileType=pathinfo($_FILES["file"]["name"],PATHINFO_EXTENSION);//Get File Extension
       
       $fileType=strtolower($fileType); //convert to lowercase
       $uploadFile=$fileName.rand(1000,10000).".".$fileType; //Set File name with Random Number
       
       //Check File Size greater than 300KB
       if($_FILES["file"]["size"]>300000){
      echo "Upload Failed.File Size is too Large!!!";
       }
       //Check File Extension
       else if($fileType != 'jpg'&&$fileType != 'jpeg'&&$fileType != 'png' && $fileType != 'gif'){
      echo "Upload Failed.Invalid File!!!";
       }
       
       //Upload File
       else{
      $imgData =addslashes (file_get_contents($_FILES['file']['tmp_name']));
      echo $sql="insert into users(image) values('{$imgData}')";
      if($con->query($sql)){
        echo "{$fileName} Upload Success";
      }
      }
    }
    ?>
    <form method='post' action='<?php echo $_SERVER["REQUEST_URI"]; ?>' enctype='multipart/form-data'>
     <input type='file' name='file' required>
     <input type='submit' value='Upload' >
    </form>
  </body>
</html>

Retrieve Blob Image from MySQL using PHP

<?php 
 $sql="select * from users";
 $res=$con->query($sql);
 if($res->num_rows>0){
   while($row=$res->fetch_assoc()){
	echo "<img src='data:image;base64,".base64_encode($row["image"])."' style='height:180px;width:200px;margin-left:22px;'>";
   }
 }
?>