title: JavaScript formdata multiple upload
body: <p>I need to upload multiple files to a server using an ajax call.</p>
  
  <p>Here is the code, every-time I am using it I am getting an upload error. The post  is not working in the html side. </p>
  
  <p>Below is the code I have used in html and php.</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>  &lt;input name="fileToUploadName"  id="fileinput0" type='file' /&gt;   
    &lt;input name="fileToUploadName"  id="fileinput1" type='file' /&gt;   
    &lt;input name="fileToUploadName"  id="fileinput2" type='file' /&gt;  
  </code></pre>
  
  <p><strong>JS</strong></p>
  
  <pre><code>function upload(){
      var formdata = new FormData();
      var image0 = $('#fileinput0')[0].files[0];
      if(image0)
         formdata.append('fileToUpload[]',image0);
  
      var image1 = $('#fileinput1')[0].files[0];
      if(image1)
         formdata.append('fileToUpload[]',image1);
  
  
      var image2 = $('#fileinput2')[0].files[0];
      if(image2)
         formdata.append('fileToUpload[]',image2);
  
  
    $.ajax({
          url: 'server.php',
          type: 'POST',
          data: formdata,
          async: true,
          dataType: "text",
          success: function (data) {
                   alert(data);
  
          },
          error: function(jqXHR, textStatus, errorThrown){
            var p =0;
             alert("Error uploading data");
           },
          cache: false,
          contentType: false,
          processData: false
      });
  
  }
  </code></pre>
  
  <p><strong>server.php</strong></p>
  
  <pre><code>if(isset($_POST['submit']))
  {
      $uploaddir = 'uploads/';
      foreach ($_FILES['fileToUpload']['error'] as $key =&gt; $error)
      {
          if ($error == UPLOAD_ERR_OK)
          {
              $tmp_name = $_FILES['fileToUpload']['tmp_name'][$key];
              $name = $_FILES['fileToUpload']['name'][$key];
              $uploadfile = $uploaddir . basename($name);
  
              if (move_uploaded_file($tmp_name, $uploadfile))
              {
                  echo "Success: File ".$name." uploaded.&lt;br/&gt;";
              }
              else
              {
                  echo "Error: File ".$name." cannot be uploaded.&lt;br/&gt;";
              }
          }
      }
  }
  </code></pre>
  
  <p>But every time I am getting an upload error. </p>