Wednesday, February 22, 2012

Upload only JPEG file using PHP


This program will upload only .jpeg file to upload. If you try to upload other than .jpeg file it will generate a error output.


<?php
 if($_FILES["file"]["type"]=='image/jpeg')
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

   if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
       move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      } 
}
else
{
   echo "Not a JPEG file.Please choose one JPEG file to upload";
}
?>
<html>
<body>
                <form action="myajax.php" method="post" enctype="multipart/form-data">
                Choose file to upload:-<input type="file" name="file">
                <input type="submit" name="sub" value="Upload">
                </form>
</body>
</html>

No comments:

Post a Comment