Firstly Thanks in Advance..

I am trying to store an image in BLOB form into database
and
want to show that image on browser on retriving it from database..

Code used to convert image to blob is given below..

ini_set('display_errors', 'On'); error_reporting(E_ALL); $img=file_get_contents("$_file[tmpname]");

but i am not able to display this image on retriveing record from database

...

Please suggest me with code for
1. image to BLOB converstion
2. BLOB to image ( on browser )

Storing images in DB is something that has been regularly discussed on DaniWeb.

Personally i would not store the actual image in the DB, only the location and file name.

This is a faster way to pull the images in.

Totally agree Squidge, I don't know why they even have blob anymore, total pain....

Thanks for your response..
But my client requirement was to store image in database

Hi,

you can easily make this happen by creating two files. Assuming that the database have the following columns..

  • id + blob_content + ext +

first, upload.php

<?php

    ## mysql connector is in sample form. I strongly suggests to use PDO wrapper
    mysql_connect("localhost","db_user","db_password");
    mysql_select_db("blob_database");


    ## define form

    $form = '<form method="post" action="" enctype="multipart/form-data">

        <input type="file" name="image" /><br/><br/>
        <input type="submit" name="upload" value="Save Image" />
    </form>'; 


    if(isset($_POST['upload'])){
    ## check for the image extension
    $image_file = file_get_contents($_FILES['image']['tmp_name']);
    $image_file = mysql_escape_string($image_file); // use PDO::quote() instead. I am using this as an example only.

    @list(, , $img_type, ) = getimagesize($_FILES['image']['tmp_name']);

    if ($img_type == 3){
        $ext="png"; 
    }
    elseif ($img_type == 2){
    $ext="jpeg";
    }
    elseif ($img_type == 1){
    $ext="gif";
    }

$query = " insert into blob_table set blob_content = '".$image_file."', ext ='".$ext."' "; //PUt your query here and make sure the $ext has its own column..
mysql_query($query);

}

else
{

echo $form;

}


## show images that are already stored in the database.. show them as thumbs

## define your query

$get_imgQ = "select * FROM blob_image";
$this_Res = mysql_query($get_imgQ);

while($row =mysql_fetch_array($this_Res)){
        ?>
            <a href="imageLoader.php?id=<?php echo $row['id']?>"><img src="imageLoader.php?id=<?php echo $row['id']; ?>" width="100" height="100"></a>
        <?php
    }
?>

row['id'] is the id of your blob image in the database..

create another file called imageLoader.php

<?php

## filename :  imageloader.php
## connect to your database
mysql_connect("localhost","db_user","db_password");
mysql_select_db("blob_database");

## process the thumb request from the upload.php
$query ="select * from blob_image where id=".$_GET['id'];

$res = mysql_fetch_array(mysql_query($query));
$this_blob_data = $res['blob_content'];

## let the browser knows what it is
header('Content-Length: '.strlen($this_blob_data));

## the extension must be known before the echo
header("Content-type: image/".$res['ext']);

## finally echo the binary blob
echo $this_blob_data;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.