Tweet
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.
Click image to view LargerImages 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
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` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `date` VARCHAR( 50 ) NOT NULL , `ip` VARCHAR( 50 ) NOT NULL , `filename` VARCHAR( 255 ) NOT NULL ) ENGINE = MYISAM ;
Here is the PHP
$uploaddir = './images/'; $file = $uploaddir.basename($_FILES['image']['name']); $file_name=$_FILES['image']['name']; if (move_uploaded_file($_FILES['image']['tmp_name'], $file)) { require_once("../../classes/config.php"); $sql=mysql_query("INSERT INTO `photoUploadDemo` (`id`, `date`, `ip`, `filename`) VALUES ('', '".date('d-m-Y')."', '".$_SERVER['REMOTE_ADDR']."', '$file_name');"); header("Location:index.php?img=".base64_encode($file_name).""); } else { header("Location:index.php?mgs=error"); }
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.