title: Multiple File upload with dynamic fields
body: <p>Can you please advise/help me with this code? I want to upload multiple files to the server and save the information in database. But this code is uploading only one file, from the first input field.</p>
  
  <p>I have dynamic "upload file" fields, like this: </p>
  
  <pre><code>              &lt;form action="uploader.php" method="POST" enctype="multipart/form-data"&gt;
                  &lt;label&gt;Upload&lt;/label&gt;
                  &lt;input type="file"  name="file[]"&gt;
                  &lt;label&gt;Description&lt;/label&gt;
                  &lt;input type="text" id="filedesc" name="filedesc[]"&gt;
                  &lt;a href="#" onClick="addUpload('dynamicInputUpload');" &gt;
                  &lt;img src="../../img/add.png" &gt;&lt;/a&gt;
          &lt;div id="dynamicInputUpload"&gt;&lt;/div&gt;
   &lt;input type="submit" float= "right"  name="add" id="add" value="UPLOAD" &gt; 
   &lt;input type="hidden" name="pid" id="pid" value="&lt;?php echo $pid; ?&gt;"&gt;
   &lt;input type="hidden" name="intnumber" id="intnumber" value="&lt;?php echo $int; ?&gt;"&gt;     
          &lt;/form&gt;    
  </code></pre>
  
  <p>Bellow is the Javascript that creates dynamic fields:</p>
  
  <pre><code> var counterUpload = 1;
   var limit = 10;
   function addUpload(divName){
   if (counterUpload == limit)  {
        alert("You have reached the limit of adding " + counterUpload + " inputs");
   }
   else {
        var newdiv = document.createElement('div');
        newdiv.innerHTML = " &lt;label&gt;File " + (counterUpload + 1) + "&lt;/label&gt;&lt;input type=\"file\" name=\"file["+counterUpload+"]\" id=\"file["+counterUpload+"]\"&gt; &lt;label&gt;Description " + (counterUpload + 1) + "&lt;/label&gt;&lt;input type=\"text\" id=\"filedesc["+counterUpload+"]\" name=\"filedesc["+counterUpload+"]\" &gt;"
        document.getElementById(divName).appendChild(newdiv);
        counterUpload++;
   }
  </code></pre>
  
  <p>}</p>
  
  <p>And the problem is bellow in the uploader.php. I suceed to uplaod only one (the first) file.  </p>
  
  <pre><code>&lt;?php
   include '../../c/config.php';
   include '../../c/opendb.php';
   // Settings
  $allowedExtensions = array('jpg','gif','bmp','png', 'docx', 'doc','pdf');
  $maxSize = 2097152;
  $storageDir = '../uploads/';
  
  // Result arrays
  $errors = $output = array();
  
  if (!empty($_FILES['file'])){  
  
    // Validation loop 
    foreach($_FILES['file']['name'] as $i=&gt; $value){
  
    $fileName = $_FILES['file']['name'][$i];
    $fileSize = $_FILES['file']['size'][$i];
    $fileErr = $_FILES['file']['error'][$i];
    $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  
    // Validate extension
    if (!in_array($fileExt, $allowedExtensions)) {
      $errors[$fileName][] = "Format $fileExt in file $fileName is not accepted";
    }
  
    // Validate size
    if ($fileSize &gt; $maxSize) {
      $errors[$fileName][] = "$fileName excedes the maximum file size of $maxSize bytes";
    }
  
    // Check errors
    if ($fileErr) {
      $errors[$fileName][] = "$fileName uploaded with error code $fileErr";
    }
  
  }
  
  // Handle validation errors here
  if (count($errors) &gt; 0) {
    die("Errors validating uploads: ".print_r($errors, TRUE));
  }
  
  // Create the storage directory if it doesn't exist
  if (!is_dir($storageDir)) {
    if (!mkdir($storageDir, 0755, TRUE)) { 
      die("Unable to create storage directory $storageDir");
    }
  }
  
  // File move loop
  foreach($_FILES['file']['name'] as $i=&gt; $array_value){
  
    // Get base info
    $fileBase = basename($_FILES['file']['name'][$i]);
    $fileName = pathinfo($fileBase, PATHINFO_FILENAME);
    $fileExt = pathinfo($fileBase, PATHINFO_EXTENSION);
    $fileTmp = $_FILES['file']['tmp_name'][$i];
  
    // Construct destination path
    $fileDst = $storageDir.'/'.basename($_FILES['file']['name'][$i]);
    for ($j = 0; file_exists($fileDst); $j++) {
      $fileDst = "$storageDir/$fileName-$j.$fileExt";
    }
  
    // Move the file    
    if (move_uploaded_file($fileTmp, $fileDst)) {
      $output[$fileBase] = "Stored $fileBase OK";
       $sql=mysql_query("INSERT INTO uploaded_files (pid, file_path,file_type)
              Values ('$_POST[pid]','$fileDst','$fileExt')") 
              or die ("Unable to issue query sql: ".mysql_error());
    } else {
      $output[$fileBase] = "Failure while uploading $fileBase!";
      $errors[$fileBase][] = "Failure: Can't move uploaded file $fileBase!";
    }
  
  }
   // Handle file move errors here
  if (count($errors) &gt; 0) {
    die("Errors moving uploaded files: ".print_r($errors, TRUE));
  } }
  </code></pre>