PHP image upload to database

By using PHP we can upload image to database. But in this article I will explain how to upload image to a web directly and inserting the name of the image into database. By following this method we have following advantages.

Image upload to database in PHP
Click image to view Larger
Images are stored in web directly, so search engine like Google can index your image and show in Google images.
Easy to upload and display.
For saving in database no need of binary or BLOB field type. By using this system it will reduce your database size.

So below I will explain you how to upload images to web directly and inserting its name to database.
You can download the example file below

Download FileDemo

First we have to design in html form and writing script for upload images.After completing the html form we have to create database for inserting images and other parameter.

Below is the Mysql,PHP and HTML file scripts

CREATE TABLE `photoUploadDemo` (
`date` VARCHAR( 50 ) NOT NULL ,
`ip` VARCHAR( 50 ) NOT NULL ,
`filename` VARCHAR( 255 ) NOT NULL

Here is the PHP

$uploaddir = './images/'; 
$file = $uploaddir.basename($_FILES['image']['name']); 
if (move_uploaded_file($_FILES['image']['tmp_name'], $file)) { 
$sql=mysql_query("INSERT INTO `photoUploadDemo` (`id`, `date`, `ip`, `filename`)
VALUES ('', '".date('d-m-Y')."', '".$_SERVER['REMOTE_ADDR']."', '$file_name');");
} else {

Download FileDemo

Upload this file and run.
While we uploading image directly the PHP will get the file and copy to web directory at the same time it will insert the name of the image to database also. So I feel this type of image upload system is good and easy to handle than the direct insert to the database by using binary or blob data type.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>