title: PHP: Uploading multiple images at the same time
body: <p>I've created an HTML form that allows me to upload an image to a uniquely named directory (i.e. a combination of date/time/ip address). Now I'm trying to upload two images (eventually multiple). I've reproduced my "File 1" content and uniquely named it to "File 2". I quickly attached a "2" to every variable and post field. Here is my upload form HTML:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;form action="icanhazuploads.php" method="post" enctype="multipart/form-data"&gt;
  Select image(s) to upload: &lt;br /&gt;&lt;br /&gt;
  &lt;input type="file" name="fileToUpload" class="fileToUpload"&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;input type="file" name="fileToUpload2" class="fileToUpload2"&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;input type="submit" name="submit"&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p>Pretty straight forward. Two upload fields and a submit button. Here is my PHP processor file:</p>
  
  <pre><code>&lt;?php
  // Display Errors
  ini_set('display_errors', 'On');
  ini_set('html_errors', 0);
  
  $ip = ip2long($_SERVER['REMOTE_ADDR']);
  $datetime = date('YmdHis');  
  $target_dir = "uploads/" . $datetime . $ip . "/";
  
  // Create the unique diretory
  if (!file_exists($target_dir)) {
      mkdir($target_dir, 0777, true);
  }
  
  // Start processing files
  
  // File 1
  echo "&lt;br /&gt;1st file being added...&lt;br /&gt;";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  echo "&lt;br /&gt;Target File: " . $target_file. "&lt;br /&gt;";
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
      $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if($check !== false) {
          echo "File is an image - " . $check["mime"] . ".&lt;br /&gt;";
          $uploadOk = 1;
      } else {
          echo "File is not an image.&lt;br /&gt;";
          $uploadOk = 0;
      }
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
      echo "Your file was not uploaded.&lt;br /&gt;";
  // if everything is ok, try to upload file
  } else {
      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
          echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.&lt;br /&gt;";
      } else {
          echo "There was an error uploading your file.&lt;br /&gt;";
      }
  }
  
  
  
  // File 2
  if(isset($_POST["fileToUpload2"])) {
  echo "&lt;br /&gt;2nd file being added...&lt;br /&gt;";
  $target_file2 = $target_dir . basename($_FILES["fileToUpload2"]["name"]);
  echo "&lt;br /&gt;Target File: " . $target_file2. "&lt;br /&gt;";
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file2,PATHINFO_EXTENSION);
  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
      $check = getimagesize($_FILES["fileToUpload2"]["tmp_name"]);
      if($check !== false) {
          echo "File is an image - " . $check["mime"] . ".&lt;br /&gt;";
          $uploadOk = 1;
      } else {
          echo "File is not an image.&lt;br /&gt;";
          $uploadOk = 0;
      }
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
      echo "Your file was not uploaded.&lt;br /&gt;";
  // if everything is ok, try to upload file
  } else {
      if (move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $target_file2)) {
          echo "The file ". basename( $_FILES["fileToUpload2"]["name"]). " has been uploaded.&lt;br /&gt;";
      } else {
          echo "There was an error uploading your file.&lt;br /&gt;";
      }
  }
  </code></pre>
  
  <p>Truly magical. I pull up my HTML form in the browser, select two images and hit submit. I can watch with Firebug or Chrome's inspector and see it with the two image fields populated via POST. However, my PHP file will consistently only see one. If I put my image only in the second field in my form, it still just acts as if I placed it in the first field. No errors in error log. Any idea why it refuses to see more than one file?</p>
  
  <p>My php.ini settings:</p>
  
  <pre><code>; Maximum allowed size for uploaded files.
  ; http://php.net/upload-max-filesize
  upload_max_filesize = 64M
  
  ; Maximum number of files that can be uploaded via a single request
  max_file_uploads = 20
  </code></pre>