How to add images in PHP


// image class to add images
// image.php
<?php
class image {
function add_image($image_name,$directory_path,$file){
$flag = false;
$name = $image_name;
// upload image for in temp folder

    $image     = $file;
    $uploadDir = $directory_path;
    // if a file is given
    if (trim($image['tmp_name']) != '') {
        // generate a random new file name to avoid name conflict
    $rand_name = md5(rand() * time()) . ".$ext";
     
// move the image to  image directory

if (move_uploaded_file($image['tmp_name'], $uploadDir . $rand_name)) {

// Here write query to insert record into database such as :
// image name , image path etc

}
     else{
      $flag = false;
    }

}
else{
$flag = false;
}




return $flag;
}

}
?>

//add.php
<?php
// call the object
include("image.php");
$image_name = $_POST['name'];
$directory_path = "image/";
$ob = new image();
 $flag= $ob->add_image($image_name,$directory_path,$_FILES['fleImage']);
if($flag){
echo "Image Added Successfully";
}
else{
echo "Operation Failed";
}
?>

// Page for image form
// test.php


<html>
<body>
<form action="add.php" method="post" enctype="multipart/form-data">
 <p><h3>Add Image</h3></p>

 <table>
  <tr>
   <td>Shape Name</td>
   <td> <input name="name" type="text"  size="30" maxlength="50"></td>
  </tr>

  <tr>
   <td>Image</td>
   <td> <input name="fleImage" type="file">
   </td>
  </tr>
<tr>
<td>
  <input name="add_shapes" type="submit"  value="Add">
 </td>
 </tr>
</table>

</form>
</body>
</html>

No comments:

Post a Comment