<?php
if(isset($_FILES["file"]["type"])){
  $validextensions = array("jpeg", "jpg", "png");
  $temporary = explode(".", $_FILES["file"]["name"]);
  $file_extension = end($temporary);
  if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg"))
    && in_array($file_extension, $validextensions)) {
     if ($_FILES["file"]["error"] > 0){
       echo json_encode(array('target'=>'','info'=>"Return Code: " . $_FILES["file"]["error"] . "<br/><br/>"));
     } else {
       if (file_exists("upload/" . $_FILES["file"]["name"])) {
         echo json_encode(array('target'=>'','info'=>$_FILES["file"]["name"] . " <span id='invalid' style='color:red;'><b>already exists.</b></span>"));
       } else {
         $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
         $targetPath = "uploads/images/".$_FILES['file']['name']; // Target path where file is to be stored
         move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file

         $info = "<span id='success' style='color:green;'><b>Image Uploaded Successfully...!!</b></span><br/>";
         $info.="<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>";
         $info.="<b>Type:</b> " . $_FILES["file"]["type"] . "<br>";
         $info.="<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
         echo json_encode(array("target"=>$targetPath,"info"=>$info));
        //  echo "<b>Temp file:</b> " . $_FILES["file"]["tmp_name"] . "<br>";
       }
     }
   } else {
     echo json_encode(array('target'=>'','info'=>"<span id='invalid' style='color:red;'><b>***Invalid file Size or Type***</b><span>"));
   }
 }
?>
