Wednesday, August 21, 2013

Store and Retrieve image from a directory using php: Part 2

 Store and Retrieve image from a directory using php: Part2

Hi !,
This is the continuation of part 1.

Create an  empty folder: upload


Create a file: upload_file_save.php 

<?php
 include 'db.inc';
 
 if (!($connection = @ mysql_pconnect($hostName,$username,$password)))
        showerror();

 if (!mysql_select_db("your dbase name", $connection))
        showerror();
        
        
        
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
//getting the prefix
$prefix=$temp[0];

//getting the extension
$picextenstion=$temp[1];

//checking
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Prefix name: " . $prefix . "<br>";
    echo "Extension: " . $picextenstion . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    

    // Re-Check connection
         if (mysqli_connect_errno($connection))
           {
             echo "Failed to connect to MySQL: " . mysqli_connect_error();
           }
         else
           {
           echo '<br> Successfully connected';

           }
          
          
      //getting the id
       $sqlid = "SELECT testid FROM testimonial ORDER BY testid DESC LIMIT 1";
       $result = mysql_query($sqlid);
       while ($getid = mysql_fetch_array($result)){
        $newid = $getid['testid'] + 1;
       }
    
    
    if (file_exists("upload/" . $newid.'.'.$picextenstion))
      {
      echo $newid.'.'.$picextenstion . " already exists. ";
      }
    else
      {
     
      //moving to the right directory
          move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $newid.'.'.$picextenstion);
      echo "\nStored in: " . "upload/" . $newid.'.'.$picextenstion;
      
       //Saving to the database.
       
       $persname=$_POST["persname"];
       $country=$_POST["country"];
       $department=$_POST["department"];
       $message=$_POST["message"];
       $picsmane=$newid.'.'.$picextenstion;

      
       echo '<br> lastest id '.$newid.'<br> ';
       
      //query
      $sql="INSERT INTO testimonial (pers_name, country, department, message, pics_name)
          VALUES ('$persname', '$country','$department','$message','$picsmane')";
          
      // Inserting into the database
        
        
         $retval = mysql_query( $sql, $connection );
        if(! $retval )
         {
           ('Could not enter data: ' . mysql_error($connection));
          }
         echo  mysql_affected_rows()." picture successfully added\n";
         echo '<a href="view.php">View the lastest picture</a><br> ';
        

          mysql_close($connection);
      
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Create a file: view.php
<?php
 include 'db.inc';

 if (!($connection = @ mysql_pconnect($hostName,$username,$password)))
        showerror();

 if (!mysql_select_db("testi", $connection))
        showerror();
?>
<html>
<body>

 <table>

  <?php
 
 $sql= "SELECT * FROM testimonial ORDER BY testid DESC ";
 $query = mysql_query($sql, $connection );
 ?>

<?php
 while($row = mysql_fetch_array($query)){
?>
 <tr>
 <td>
 <label for="">ID: </label>
 </td>
 <td><?php echo $row['testid']; ?></td>
 </tr>
 <tr>
 <td>
 <label for="">Name</label>
 </td>
 <td><?php echo $row['pers_name']; ?></td>
 </tr>
 <tr>
 <td>
 <label for="">Picture</label>
 </td>
 <td><img height='100' width='200' src=" <?php echo 'upload/'.$row['pics_name'];?>"/></td>
 </tr>
<?php
 }
 ?>



 </table>
<?php
 echo '<br> <a href="index.php">Enter another picture</a><br> ';
 ?>
</body>
</html>

After this point your will have the following files.

No comments:

Post a Comment