INSERT `BLOB` IMAGES ON YOUR MYSQL DATABASE USING PHP


Code Samples :

1. Create the database and tables

2. index.php







<?php
if(isset($_POST['submit']))
{
mysql_connect("127.0.0.1","root","");
mysql_select_db("image_db");

$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);

if(substr($imageType,0,5) == "image")
{
mysql_query("INSERT INTO `image` VALUES('','$imageName','$imageData')");
echo "Image Uploaded!";
}
else
{
echo "Only images are allowed";
}
}

?>




3. showImage.php


<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("image_db");

if(isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$query =mysql_query("SELECT * FROM `image` WHERE `id`='$id'");
while($row = mysql_fetch_assoc($query))
{
$imageData = $row["image_file"];
}
header("content-type: image/jpeg");
echo $imageData;
}
else
{
echo "Error!";
}
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s