title: ajax multiple file upload with php
body: <p>Hey I am uploading files to a chosen folder and right now I have the ability to select and upload just one file.  I know how to handle multiple files in php but I am not sure how to send all of the files over through AJAX. Thanks for any help you can offer</p>
  
  <p>AJAX</p>
  
  <pre><code> function submitForm() {
              console.log("submit event");
              var fd = new FormData(document.getElementById("fileinfo"));
              fd.append("label", "sound");
              fd.append('label', document.getElementById('selected_folder').value);
              $.ajax({
                url: "upload.php",
                type: "POST",
                data: fd,
                enctype: 'multipart/form-data',
                processData: false,  // tell jQuery not to process the data
                contentType: false   // tell jQuery not to set contentType
              }).done(function( data ) {
                  console.log("PHP Output:");
                  console.log( data );
                  alert("upload success!")
              });
              return false;
          }
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  if ($_POST["label"]) {
      $subfolder = $_POST["label"];
  }
  
  
  $allowedExts = array("gif", "jpeg", "jpg", "png");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);
  if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/jpg")
  || ($_FILES["file"]["type"] == "image/pjpeg")
  || ($_FILES["file"]["type"] == "image/x-png")
  || ($_FILES["file"]["type"] == "image/png"))
  &amp;&amp; ($_FILES["file"]["size"] &lt; (10000*1024))
  &amp;&amp; in_array($extension, $allowedExts)) {
      if ($_FILES["file"]["error"] &gt; 0) {
           // echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br&gt;";
      } else {
          $filename = $_FILES["file"]["name"];
          echo "Upload: " . $_FILES["file"]["name"] . "&lt;br&gt;";
          echo "Type: " . $_FILES["file"]["type"] . "&lt;br&gt;";
          echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB&lt;br&gt;";
          echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br&gt;";
  
  
  
          if (file_exists("uploaded/".$subfolder .'/'. $filename)) {
              //already exists 
          } else {
              move_uploaded_file($_FILES["file"]["tmp_name"],
              "uploaded/".$subfolder .'/'. $filename);
              // "Stored in: " . "uploaded/".$subfolder .'/'. $filename;
          }
      }
  } else {
      echo "Invalid file";
  }
  ?&gt;
  </code></pre>