Simple file Upload PHP script

One of the most popular features in php is uploading files , php can easily access to computer storage,get the file , and store it for another uses .
php file upload

The script below serve to upload an image with special extensions (jpeg,png,gif) and maximum size of 100 KB.



 

 

Html Upload Form :


    



PHP Upload Script :

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
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"] < 100000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "
";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "
";
    echo "Type: " . $_FILES["file"]["type"] . "
";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";

    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: " . "Pictures/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }


The script will store the photo into the "Pictures" folder after uploading it , Or it will show an error message "Invalid file" if the file does not match the required extensions (jpeg,png,gif) or its size is Heighter than 100000 (100KB). 
Please, try it and let me know -in your comment- if you have any problem .


0 comments

Posts a comment

 
© Articmo, web development blog | Api tutorial | seo | javascript | html | php

All rights reserved the content is copyrighted to Mohamed Mbarki.

Masters scholarships
Back to top