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:

  • TINYBLOB

  • BLOB

  • MEDIUMBLOB

  • LONGBLOB

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">

</form>

 

3) Create upload.php page

<?php

if(!isset($_FILES['userfile']))

{

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

}

else

{

    try {

    $msg= upload(); 

    echo $msg; 

    }

    catch(Exception $e) {

    echo $e->getMessage();

    echo 'Sorry, could not upload file';

    }

}

function upload() {

require_once('file_constants.php');

mysql_set_charset('utf8');

 

    $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());

                    ?>

<script>

alert('Recored Update successfully');

location.replace('index.php')</script>

<?php

            }

             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 />';

                }

        }

        else

            $msg="File not uploaded successfully.";

    }

    else {

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

    }

?>

<script>

alert('File size is large');

location.replace('index.php')</script>

<?php

    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';

        case UPLOAD_ERR_CANT_WRITE:

            return 'Failed to write file to disk';

        case UPLOAD_ERR_EXTENSION:

            return 'File upload stopped by extension';

        default:

            return 'Unknown upload error';

    }

}

?>

 

4) Create Display.php page

<?php

$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); 

}

}

$conn->close();

?>


Output

                   

Support us by Sharing