title: PHP form validation of uploaded file
body: <p>I'm having trouble validating a file upload in php. Basically the code doesn't see that the files are empty, I've been looking at it all afternoon and can't figure it out. I'm sure it's something simple. If somebody could solve my problem I'd greatly appreciate it. Thanks in advance.....</p>
  
  <p>ps. this is in an unfinished project, I'm just stuck at the validation stage.</p>
  
  <pre><code>&lt;?php
      // Link to separate file that holds the connection info
      include 'connection.php';
  
      $error = "";
  
      if ($_POST){
  
          // Back up to JS Validation
          if(!$_POST['username']){
              $error = "No username&lt;br&gt;";
          }
          if(!$_POST['password']){
              $error .= "No password&lt;br&gt;";
          }
          if(!$_POST['email']){
             $error .= "No email&lt;br&gt;";
          }
  
          // Test to see if a file has been posted
          if(!empty($_FILES['uploaded_file'])){
              // Set the upload path
              $path = "uploads/";
              // Append the filename
              $path = $path . basename( $_FILES['uploaded_file']['name']);
  
              // Need to move the file to folder
              // If successful display success message or error
              if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
                  // File has been uploaded create user
                  $query = "INSERT INTO ";
              } else {
                  // File not uploaded, account not created
                  echo "There was an error uploading the file, You have not create an account";
              }
          } else {
              $error .= "No file uploaded";
          }
      } 
  ?&gt;
  
  &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;!-- Required meta tags --&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
  
      &lt;!-- Bootstrap CSS --&gt;
      &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"&gt;
  
      &lt;style&gt;
  
          h2 {
              margin-top: 50px;
          }
  
          .signupContainer {
              background-color: darkgray;
              width: 400px;
              border-radius: 10px;
              margin: auto;
              margin-top: 20px;
          }
          .alertContainer {         
              width: 400px;
              margin: auto;
              margin-top: 20px;
          }
  
          .signupFormContainer {
              padding: 20px 50px 20px 50px;
              margin: auto; 
          }
  
      &lt;/style&gt;
  
      &lt;title&gt;NotePad App&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
  
      &lt;h2 class="text-center"&gt;NotePad App&lt;/h2&gt;  
  
      &lt;?php
        // Back up to JS validation - display errors
        if ($error){
            ?&gt;
              &lt;div class="alertContainer"&gt;
                  &lt;div class="alert alert-danger"&gt;
                    &lt;strong&gt;Error!&lt;/strong&gt; There were errors on your form:&lt;br&gt;&lt;?php echo $error ?&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
            &lt;?php
        }
  
      ?&gt;
  
      &lt;div class="signupContainer"&gt;
          &lt;div class="signupFormContainer"&gt;
              &lt;form enctype="multipart/form-data" action="index.php" method="POST" id="createUser"&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="username"&gt;Username&lt;/label&gt;
                    &lt;input type="text" class="form-control" id="username" placeholder="Create a Username" name="username"&gt;
                    &lt;div class="invalid-feedback"&gt;
                      Please enter a username
                    &lt;/div&gt;
                &lt;/div&gt; 
                &lt;div class="form-group"&gt;
                    &lt;label for="password"&gt;Password&lt;/label&gt;
                    &lt;input type="password" class="form-control" id="password" placeholder="Enter a Password" name="password"&gt;
                    &lt;div class="invalid-feedback"&gt;
                      Please enter a password
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="password"&gt;Email Address&lt;/label&gt;
                    &lt;input type="email" class="form-control" id="email" placeholder="Enter your email address" name="email"&gt;
                    &lt;div class="invalid-feedback"&gt;
                      Please enter a valid email address
                    &lt;/div&gt;
                &lt;/div&gt;  
                &lt;div class="form-group"&gt;
                    &lt;label for="password"&gt;Upload Text File&lt;/label&gt;
                    &lt;input class="form-control" type="file" id="uploaded_file" name="uploaded_file" accept="text/plain"&gt;
                    &lt;div class="invalid-feedback"&gt;
                      Please select a txt file
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="form-group text-center"&gt;
                    &lt;button type="button" class="btn btn-success" id="signup"&gt;Sign Up!&lt;/button&gt;
                &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
      &lt;!-- Optional JavaScript --&gt;
      &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;
      &lt;script
    src="https://code.jquery.com/jquery-3.3.1.js"
    integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
    crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"&gt;&lt;/script&gt;
  
      &lt;script type="application/javascript"&gt;
  
          function clearClass(){
              $("#username").removeClass("is-invalid");
              $("#password").removeClass("is-invalid");
              $("#email").removeClass("is-invalid");
               $("#uploaded_file").removeClass("is-invalid");
          }
  
          $("#signup").click(function(){
  
              clearClass();
              var frmError = false;
  
  //            if ($("#username").val() == ""){
  //                $("#username").addClass("is-invalid");
  //                frmError = true;
  //            }
  //            if ($("#password").val() == ""){
  //                $("#password").addClass("is-invalid");
  //                frmError = true;
  //            }
  //            if ($("#email").val() == ""){
  //                $("#email").addClass("is-invalid");
  //                frmError = true;
  //            }
  //            if ($("#uploaded_file").val() == ""){
  //                $("#uploaded_file").addClass("is-invalid");
  //                frmError = true;
  //            }
  
              if (!frmError){
                  $('#createUser').submit();
              }
          });
  
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>