Google SEO | PHP | Mysql | Ajax | Jquery | Photoshop | Illustrator | Database | Web Design | Web Development | Sitemap |

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.

Top Λ

Comments (14)

  • jaspreet kaur

    Great Article,,it works,,sir can you post dis code using by classes? and i also want dis code is validate only like .jpeg,.gif,.png
    Wednesday-Dec-2012 | 12:54:50 PM
  • Admin

    Thank you all..
    Saturday-Nov-2012 | 12:29:35 PM
  • raoof t

    really nice your frame work.
    Saturday-Nov-2012 | 03:27:57 AM
  • Senathissa

    Actually you may lot of merits.. I'm highly appreciated. it works.. Thanks a lot...
    Thursday-Oct-2012 |
  • hemal

    Nice post. Please give me code for videos upload using database.
    Monday-Jul-2012 | 9:11:41 PM
  • Govnopoisk

    Good post!
    Thursday-May-2012 |
  • Govnopoisk

    Good post!
    Thursday-May-2012 |
  • Govnopoisk

    Good post!
    Thursday-May-2012 |
  • Michelle Lee

    Perfect post.
    Saturday-Feb-2012 | 6:36:30 PM
  • Admin

    Config file is nothing but mysql connection. Simply connect your mysql database like below:
    Monday-Oct-2011 | 3:29:16 PM
  • Admin

    This is the common error displaying in localhost. Upload it in web server it will not show. These are not errors!!
    Monday-Oct-2011 | 3:25:50 PM
  • muzammil

    I GOT THESE ERRORS: Notice: Undefined index: img in C:\wamp\www\upload\index.php on line 55 Notice: Undefined index: mgs in C:\wamp\www\upload\index.php on line 72
    Tuesday-Oct-2011 | 7:53:2 PM
  • Admin

    config.php is nothing but your DB connection.
    Monday-Oct-2011 | 10:0:24 AM
  • jayr

    where is the codes config.php
    Wednesday-Oct-2011 | 1:24:52 AM

Post Comment