<?php

// we'll upload the files to this folder
$image_folder = "uploaded-files/";

if($_SERVER['REQUEST_METHOD'] == "POST"){

  //var_dump($_FILES);
 // die("Goodbye");
  $error_message = "";
  if($_FILES['upload']['error'] == UPLOAD_ERR_OK){
    //we have a file on the server
    //These are the allowed file types:
    $allowed = array('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png');
    
    if(in_array($_FILES['upload']['type'], $allowed)){
      // move file from tmp directory to it's proper folder
      $tmp_file_name = $_FILES['upload']['tmp_name'];
      $file_name = $_FILES['upload']['name'];
      if(move_uploaded_file($tmp_file_name, $image_folder . $file_name)){
        echo("File has been uploaded!");
      }
  }else{
    $error_message = "That file type is not allowed!";

  }
  }else{
    $error_message = "No file Posted!";
  }
  if(!empty($error_message)){
    echo($error_message);
  }
}

?>
<h1>Uploading Files</h1>
Some things to remember:
<ul>
  <li>Use &lt;input type="file" /&gt; to allow file uploads</li>
  <li>Make sure to set the 'enctype' attribute on the form tag (otherwise the file will not be uploaded)</li>
  <li>Make sure that PHP is configured properly for file uploads (check the upload_max_filesize setting in the php.ini file)</li>
  <li>To be safe, uploaded files should go outside of the doc root dir, but that presents some problems if you want to be able to link the the files from your pages.</li>
</ul>

<br><br>
<!--enctype="multipart/form-data" specifies that the form should be able to handle multiple types of data-->
<form method="POST" action="<?php echo($_SERVER['PHP_SELF']);?>" enctype="multipart/form-data">
	<div class="row">
      <div class="label">
  			Image:
  		</div>
  		<div class="control">
        <!--<input type="hidden" name="MAX_FILE_SIZE" value="50000" />-->
  			<input type="file" name="upload" />
  		</div>
 	</div>
  <div class="row">
      <div class="label">

      </div>
      <div class="control">
        <input type="submit" name="btnSubmit" value="SAVE" />
      </div>
  </div>
</form>
