title: PHP multiple file upload uploads same image 5 times instead of all files
body: <p>Hey guys I am really struggling with my code. When I try to upload multiple images it uploads the same image 5 times instead of uploading both images. Any help is appreciated.
  Thank you for your time.</p>
  
  <p>Here is the var_dumb($_FILES)</p>
  
  <pre><code>array(1) {
    ["userfile"]=&gt;
    array(5) {
      ["name"]=&gt;
      string(9) "test1.jpg"
      ["type"]=&gt;
      string(10) "image/jpeg"
      ["tmp_name"]=&gt;
      string(14) "/tmp/phpdDGCy2"
      ["error"]=&gt;
      int(0)
      ["size"]=&gt;
      int(1768037)
    }
  }
  </code></pre>
  
  <p>Here is my upload function:</p>
  
  <pre><code>if(count($_FILES['userfile'])){
      foreach($_FILES['userfile'] as $file){
  $alt=mysqli_real_escape_string($conn, $_POST['alt']);
  echo '&lt;pre&gt;';
  var_dump($_FILES);
  echo '&lt;/pre&gt;';
  if (($_FILES["userfile"]["error"] == 0) &amp;&amp; ($_FILES['userfile']['size'] &gt; 0))
  {
  $fileName = $_FILES['userfile']['name'];
  $tmpName  = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  $fileType = $_FILES['userfile']['type'];
  } 
  $allowedExts = array("jpg", "jpeg", "gif", "png");
  $extension = end(explode(".", $_FILES["userfiles"]["name"]));
  if((($_FILES["userfile"]["type"] == "image/gif")
      ||($_FILES["userfile"]["type"]=="image/jpeg")
      ||($_FILES["userfile"]["type"]=="image/png")
      ||($_FILES["userfile"]["type"]=="image/pjpeg")
      &amp;&amp; in_array($extension, $allowedExts)))
      {
          $fp = fopen($tmpName, 'r');
          $content =fread($fp, filesize($tmpName));
          $SourceImage = imagecreatefromstring($content);
          $SourceWidth = imagesx($SourceImage);
          $SourceHeight=imagesy($SourceImage);
          $DestWidth=100;
          $DestHeight=130;
          if ($SourceHeight&gt; $SourceWidth)
          {$ratio = $DestHeight / $SourceHeight;
          $newHeight = $DestHeight;
          $newWidth = $sourceWidth * $ratio;
          }
          else
          {
              $ratio = $DestWidth / $SourceWidth;
              $newWidth = $DestWidth;
              $newHeight = $SourceHeight * $ratio;
          }
          $DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
          imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
          ob_start();
          imagejpeg($DestinationImage);
          $BinaryThumbnail = ob_get_contents();
          ob_end_clean();
          $thumb = addslashes($BinaryThumbnail);
          $content = addslashes($content);
          fclose($fp);
          $fp      = fopen($tmpName, 'r');
  $content = fread($fp, filesize($tmpName));
  $content = addslashes($content);
  fclose($fp);
  
           mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed'); 
             echo "&lt;script&gt;alert('The file has been uploaded');location.replace('uploaded.php');&lt;/script&gt;";
             unlink ($_FILES['username']['tmp_name']);
  
      }else{ 
             echo "&lt;script&gt;alert('Please upload an image');location.replace('upload.php');&lt;/script&gt;";
      }
  }
  }
  }
  </code></pre>
  
  <p>Here is upload form:</p>
  
  <pre><code>    &lt;div class="container"&gt;
   &lt;div class="row"&gt;
    &lt;div class="col-md-6 col-md-offset-3"&gt;
            &lt;h1&gt;Upload a file&lt;/h1&gt;
  &lt;form method="post" enctype="multipart/form-data" action="process.php"&gt;
  &lt;div id="filediv"&gt;
  &lt;div id="imagefiles"&gt;
  &lt;input type="hidden" name="MAX_FILE_SIZE" value="2000000"&gt;
  &lt;label&gt;Upload File:
  &lt;input name="userfile[]" type="file" id="userfile" multiple&gt;&lt;/label&gt;
  &lt;label&gt;Alt Text: &lt;input name="alt" type="text"&gt;&lt;/label&gt;
   &lt;/div&gt;
   &lt;/div&gt;
  &lt;br&gt;
  
  &lt;input type="button" value="Add Another File" onclick="copyDiv()"/&gt;
  &lt;input name="UploadFile" type="submit" /&gt;
  &lt;/form&gt;
  </code></pre>