How to Upload and Insert Image to Database using PHP

Authors: CodeToday | PHP Code | Views: 38 | Posted: 03 AM: 10/04/2017

When you upload an image to the server, there are two ways to store the image:
  1. Upload and Save to a folder on the server.
  2. Upload and Save directly to the database
Each has its own advantages and disadvantages, in this article I will guide how to Upload and Insert Image to Database using PHP.

The advantage of this method is that the data set is generally in the database should move the website easily, without losing image links. The downside is that if you store a lot of images in the database the size of the database will be very large, making the data access is slow.

The steps are as follows:

  1. Create a database to store the image and name of the image
  2. Create a file that connects to the database
  3. Upload image and store them in the database
  4. Display the image taken from the database

Database

<?
mysql_query("DROP TABLE IF EXISTS `img_database`;
CREATE TABLE `img_database` (
  `id` int(11) NOT NULL auto_increment,
  `img` mediumblob,
  `img_name` varchar(255) collate utf8_unicode_ci default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
?>

"img" field you can choose the type of data storage: blob, mediumblob, longblob. Because image data is stored as binary data

PHP Source Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to Upload and Insert Image to Database using PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-primary">How to Upload and Insert Image to Database using PHP</h1>
<form action="" method="post" class="form-inline" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="image" value="<?=$_POST['image']?>" size="80" class="form-control" required/>
</div>
<div class="form-group">
<button class="btn btn-info" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i> UPLOAD IMAGE</button>
</div>
</form>
<?
if(isset($_POST['submit'])) {

include 'connect.php';
$image_name = $_FILES['image']['name']; 
$image_file = addslashes (file_get_contents($_FILES['image']['tmp_name']));
mysql_query("INSERT INTO img_database SET img = '".$image_file."', img_name = '".$image_name."'");
}
echo '<h3 class = "text-primary">Show Result</h3>';
echo '<img src = "get_image.php?name=test.png" class = "img-thumbnail">';
?>
</div>
</body>
</html> 

get_image.php

<?php
header("content-type:image/jpeg");
include 'connect.php';
$img_name = $_GET['name'];
$sql = mysql_query("SELECT * FROM img_database WHERE img_name = '$img_name'");
if(mysql_num_rows($sql)) {
$row = mysql_fetch_array($sql);
	echo $row['img'];
}
?>

connect.php

<?
mysql_connect("localhost", "root", "admin") or die("Could not connect"); 
mysql_select_db("code") or die("Could not select database"); 
?>

Here, we test with the image named "test.png". After performing the above steps you can see the results as below

How to Upload and Insert Image to Database using PHP

If you want to display multiple images, you can create a loop through the database fields and then extract the image name and display.

$img_fields = array($row1,$row2,$row3, ...);
foreach($img_fields as $img_field) {
    echo '<img src = "get_image.php?name=$img_field" class = "img-thumbnail">';
}

If you have any questions, please leave a message below