Tuesday, 16 September 2014

upload an image and view it using php

save as img.php  to get a form of upload

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

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>

</tr>

<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>


</tbody></table>


</form>

Save as saveimage.php

 <?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image";
$user="root";
$pass="";
/**********MYSQL Settings****************/


$conn=mysql_connect($host,$user,$pass);

if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
   die('Not connected : ' . mysql_error());
}
?>

<?php /*
    function GetImageExtension($imagetype)
   {
      if(empty($imagetype)) return false;
      switch($imagetype)
      {
          case 'image/bmp': return '.bmp';
          case 'image/gif': return '.gif';
          case 'image/jpeg': return '.jpg';
          case 'image/png': return '.png';
          default: return false;
      }

    } */
?>

 
    <?php
error_reporting(0);
//include("mysqlconnect.php");

   function GetImageExtension($imagetype)
    {       if(empty($imagetype)) return false;
      switch($imagetype)
      {
          case 'image/bmp': return '.bmp';
          case 'image/gif': return '.gif';
          case 'image/jpeg': return '.jpg';
          case 'image/png': return '.png';
          default: return false;
      }
     }
   
   
   
if (!empty($_FILES["uploadedimage"]["name"])) {

   $file_name=$_FILES["uploadedimage"]["name"];
   $temp_name=$_FILES["uploadedimage"]["tmp_name"];
   $imgtype=$_FILES["uploadedimage"]["type"];
   $ext= GetImageExtension($imgtype);
   $imagename=date("d-m-Y")."-".time().$ext;
   $target_path = "C:/wamp/www/imagefetch/IMAGE/".$imagename;
 

if(move_uploaded_file($temp_name, $target_path)) {

   $query_upload="INSERT INTO `images_tbl`(`images_path`,`submission_date`) VALUES

('".$target_path."','".date("Y-m-d")."')";

   mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());
echo "uploaded successfully";
 
}else{

  exit("Error While uploading image on the server");
}

}

?>
<?php
///Code to use image name with new customized  image name .
$imagename=date("d-m-Y")."-".time().$ext;

///Code to use image name as same as uploaded image name .
$imagename=$_FILES["uploadedimage"]["name"];
?>


  Save as view.php to view image

<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="image";
$user="root";
$pass="";
/**********MYSQL Settings****************/


$conn=mysql_connect($host,$user,$pass);

if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
   die('Not connected : ' . mysql_error());
}
?>



  <?php
//include("mysqlconnect.php");

$select_query = "SELECT `images_path` FROM  `images_tbl` ORDER BY `images_id` DESC";
$sql = mysql_query($select_query) or die(mysql_error());
while($row = mysql_fetch_array($sql)){

?>

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellpadding="5" cellspacing="5">
<tbody><tr>
<td>

<img src="<?=$row['images_path`']?>">

</td>
</tr>
</tbody></table>

<?php
}

?>

No comments:

Post a Comment