How to upload and retrieve blob image using php

21/05/2017   10:15:41 AM

Hello, in this tutorial I will explain how to upload and show blob image in php. Image Uploading is very easy there are two ways you can upload image either to the database or in folder and save path in database. In this tutorial we see how to upload image in database.

MySQL has a blob data type which can used to store binary data. A blob is a collection of binary data stored as a single entity in a database managemensystem. Blobs are typically images, audio or other multimedia blob objects. MySQL has four BLOB types:


  • BLOB



All these types differ only in their sizes.

1) Create a table named upload_image. The table will contain two fields one is id (auto increment, and primary key) and other is img of datatype blob for storing image. As shown in below


2) Create index.php page

<form method="post" action="upload.php" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="900000"/><input name="userfile" type="file" style="height:35px;" /><br><br>

<input type="submit" value="submit">



3) Create upload.php page




    echo '<p>Please select a file</p>';




    try {

    $msg= upload(); 

    echo $msg; 


    catch(Exception $e) {

    echo $e->getMessage();

    echo 'Sorry, could not upload file';



function upload() {




    $maxsize = 100000;

    if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {

        if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {   

                if( $_FILES['userfile']['size'] < $maxsize) { 

                      $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

                    mysql_connect($host, $user, $pass) OR DIE (mysql_error());


                     mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());

                    // our sql query

                  $sql = "INSERT INTO upload_image (img) VALUES ('{$imgData}');";



                    mysql_query($sql) or die("Error in Query: " . mysql_error());



alert('Recored Update successfully');




             else {

                       $msg='<div>File exceeds the Maximum File limit</div>

                <div>Maximum File limit is '.$maxsize.' bytes</div>

                <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].

                ' bytes</div><hr />';




            $msg="File not uploaded successfully.";


    else {

                   $msg= file_upload_error_message($_FILES['userfile']['error']);




alert('File size is large');



    return $msg;


function file_upload_error_message($error_code) {

    switch ($error_code) {

        case UPLOAD_ERR_INI_SIZE:

            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';

        case UPLOAD_ERR_FORM_SIZE:

            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';

        case UPLOAD_ERR_PARTIAL:

            return 'The uploaded file was only partially uploaded';

        case UPLOAD_ERR_NO_FILE:

            return ' ';

        case UPLOAD_ERR_NO_TMP_DIR:

            return 'Missing a temporary folder';


            return 'Failed to write file to disk';


            return 'File upload stopped by extension';


            return 'Unknown upload error';





4) Create Display.php page


$id = $_GET['id'];

$conn = new mysqli("localhost","root","","sampledb");

$sql = "SELECT * FROM upload_image where id= '$id'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

header('content-type: image/jpeg');

echo $a=$row['img'];

echo base64_decode($a); 







Support us by Sharing