Question


Amit

How to upload image and store in database using PHP ?

Date = 2015-03-24

<form action="upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="image"  >
<input name="Submit" type="submit" value="Upload" />
</form>

Comments by experts


Rajesh Kakadiya
2015-03-24

<?php 

$con
=mysql_connect("localhost","username","password"); // connection to localhost
if(!$con)
  {
  die(
'Could not connect :'.mysql_error()); // showing error if having problem with username or password
  
}
mysql_select_db("database_name",$con); // select database from localhost
 
if ($_FILES["image"]["error"] > 0)
  {
     echo 
"No File choosen ! Please select file first.";
   }
   else
   {
move_uploaded_file($_FILES["image"]["tmp_name"],"images/".$_FILES["image"]["name"]); //put images folder. It will move image to this folder

     
$file_path="images/".$_FILES["image"]["name"];
     
$sql="INSERT INTO table_name (path) VALUES ('$file_path')";

     if (!
mysql_query($sql))
     {
        die(
'Error: '.mysql_error());
     }
     echo 
"path added into databse successfully";

   }
   
mysql_close();
?>


Type your Comment below and click to send comment