
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Upload New Image</title>
    <style media="screen">
      body {background:#eee;padding:100px;}
      #wrapper {background:#fff;border:1px solid #ccc;border-radius:15px;padding:20px;}
    </style>
  </head>
  <body>
  <div id="wrapper">
    <?php
    if(isset($_POST['submit_image']))
    {


      /* ----- File Upload --- */

      $imagefolder = 'uploads/';

      $target_file = $imagefolder . basename($_FILES["fileToUpload"]["name"]);
      $uploadOk = 1;
      $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
      $renamemyphoto = 0;


      $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

      if($check !== false) {
          $uploadOk = 1;
      } else {
          $uploadOk = 0;
      }


      // Check if file already exists
      if (file_exists($target_file)) {
          $renamemyphoto = 1;
          $randoo = rand(22222, 99999);
      }

      // Check file size
      if ($_FILES["fileToUpload"]["size"] > 500000) {
          echo "Sorry, your file is too large.";
          $uploadOk = 0;
      }

      // Allow certain file formats
      if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
          echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
          $uploadOk = 0;
      }

      // Check if $uploadOk is set to 0 by an error
      if ($uploadOk == 0) {
          echo "Sorry, your file was not uploaded.";

      // if everything is ok, try to upload file
      } else {


      if($renamemyphoto == 1){

        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imagefolder.$randoo.'-'.basename($_FILES["fileToUpload"]["name"]))) {
            echo $imagefolder.$randoo.'-'.basename($_FILES["fileToUpload"]["name"]);
        } else {
            echo "Sorry, there was an error uploading your file.";
        }

      }else{

          if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imagefolder.basename($_FILES["fileToUpload"]["name"]))) {
              echo $imagefolder.basename( $_FILES["fileToUpload"]["name"]);
          } else {
              echo "Sorry, there was an error uploading your file.";
          }
        }

     exit();

    }

  } else {
    ?>
   <form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="fileToUpload" name="fileToUpload" />
    <input type="submit" name='submit_image' value="Upload Image"/>
   </form>
 <?php } ?>
  </div>
  </body>
</html>
