PHP Image Upload To SQL Database By Using BLOB Data Type

You can save some times if you upload images through PHP and store it in your SQL database.

BLOB is Binary Large Object. The image that's been uploaded will be stored inside your SQL database in the form of binary. This kind of way saves your time. But, storing image in SQL database with BLOB is not always a good choice. Too many images that need to be loaded from your SQL might give impact to your database's loading time. So, please make a wise choice before choosing to store the images inside the SQL database. There are times where you should store the image into the SQL database and there are other times where you shouldn't to.
Before making any confusions especially for those who are still new in web programming and database, I would like to remind you of something. SQL (Structured Query Language) is programming language where we use it to do data manipulation in database. I believe some of you have heard about "MySQL". MySQL is one of RDBMS (Relational Database Management System) softwares that use SQL in it. So, SQL is programming language and MySQL is software. You may use other softwares such as Oracle or PostgreSQL and etc.
Ok. It is time for the tutorial. Please follow the following steps:

1. New Database
You need to make new database "upload" or any other names.
2. New Table
After that, you have to make new table inside the new database. Please make one new table "store" or any other names. The table should has the following structures:
Table "store":
CREATE TABLE store (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
image BLOB NOT NULL,
PRIMARY KEY (id)
);

3. PHP
We will make index.php ; img.php ; get.php
index.php:
<html>
<head>
<title>Upload</title>
</head>

<body>

<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" accept="image/*"><!--We will allow any type of images such as jpg or png or etc-->
<input type="submit" name="submit" value="Upload">
</form>

<?php
error_reporting(E_ALL ^ E_NOTICE);
//connect to database
mysql_connect("localhost","root","pwd") or die (mysql_error());
mysql_select_db("upload") or die (mysql_error());

if(isset($_POST['submit']))
    {
    //file properties
    $file=$_FILES['image']['tmp_name'];
    $image_name=mysql_real_escape_string($_FILES['image']['name']);
    $image=mysql_real_escape_string(file_get_contents($file));
    $image_size=getimagesize($file);
    
        if($image_size==FALSE)
            {
            echo "That is not an image";
            }
        else
            {
            $insert=mysql_query("INSERT INTO store (name,image) VALUES ('$image_name','$image')");
                if($insert)
                {
                    echo "OK!";
                    $lastid=mysql_insert_id();
                    echo "Image<p/>Your image: <img src=get.php?id=$lastid>";
                }
                else
                {
                    echo "PROBLEM!";
                }
            }
    
    }
else
    {
        echo "That is not an image!";
    }

?>

</body>

</html>

img.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost","root","pwd") or die (mysql_error());
mysql_select_db("upload") or die (mysql_error());

$query = mysql_query("SELECT * FROM store");
  while($row=mysql_fetch_array($query))
  {
    echo $id=$row['id'];
    echo ". ";
    echo $row['name'];
    echo " ";
    echo "<img src=get.php?id=$id>";
    echo "<br/>";
  }

?>

get.php:
<?php

mysql_connect("localhost","root","pwd") or die (mysql_error());
mysql_select_db("upload") or die (mysql_error());

$id=mysql_real_escape_string($_REQUEST['id']);

$select=mysql_query("SELECT * FROM store where id=$id");
$image_fetch=mysql_fetch_assoc($select);
$image=$image_fetch['image'];

header("Content-type: image/*");

echo $image;

?>

All set? You can access index.php to start uploading new image. After that, open img.php The view the images that have been uploaded.
That's all and Thank you. Good luck...

Labels: , , , , , , , ,