<?php
DEFINE
('UPLOAD_DIR''uploads/');

if(
$_GET["list"] == "1")
  
$mode "list";
else
  
$mode "gallery";

if(
$mode == "list") {
  
$files = [];
  if (
is_dir(UPLOAD_DIR)) {
      if (
$dh opendir(UPLOAD_DIR)) {
          while ((
$file readdir($dh)) !== false) {
            if (
substr($file01) != ".") {
              
$files[] = array( "filename" => UPLOAD_DIR $file"uploaded" => filemtime(UPLOAD_DIR $file));
            }
          }
          
closedir($dh);
      }
  }
  
header("Content-Type: application/json");
  echo 
json_encode($files);
} else {
  if(
$_FILES['photo']['name']) {
    if(
$_POST['password'] == 'wibergsupersecure') {
      
//if no errors...
      
if(!$_FILES['photo']['error'])
      {
        
$valid_file true;
      
        
//now is the time to modify the future file name and validate the file
        
$new_file_name strtolower($_FILES['photo']['tmp_name']); //rename file
      
        
if($_FILES['photo']['size'] > (4096000)) //can't be larger than 4 MB
        
{
          
$valid_file false;
          
$message 'Oops!  Your file\'s size is to large.';
        }
      
        
//if the file has passed the test
        
if($valid_file)
        {
          
$parts pathinfo($_FILES['photo']['name']);
        
          
//move it to where we want it to be
          
move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_DIR basename($_FILES['photo']['tmp_name']) . "." $parts["extension"]);
        }
      }
      
//if there is an error...
      
else
      {
        
//set that to be the returned message
        
$message 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
      }      
    } else {
      
$message 'Wrong password, klutz!';
    }
    
    if(isset(
$message)) {
      
// Something went wrong!
      
echo $message;
      exit();
    }
    
    
header("Location: " $_SERVER['PHP_SELF'] . "?list=1");
  } else {
?>
    <!DOCTYPE html>
    <html>
    <head>
      <title>Simple Uploader</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    <body>
      <div class="container">

      <h1>Simple Uploader</h1>
      <form action="?upload=1" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <label for="photo">Photo plox:</label>
          <input type="file" class="form-control" accept="image/*" capture="camera" id="photo" name="photo" size="25" />
        </div>
        
        <div class="form-group">
          <label for="password">Password:</label>
          <input type="password" class="form-control" id="password" name="password" size="25" />
        </div>
        
        <div class="form-group">
          <input type="submit" name="submit" value="Submit" class="btn btn-primary"/>
        </div>
      </form>


          </div><!-- /.container -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </body>
    </html>
  <?php
  
}
}
?>