title: Uploading multiple files to mySQL then adding to Session
body: <p>Okay so to explain what I am trying to achieve, I want a user to be able to do a multiple file upload (specifically for images) without having multiple file input fields, so I have used a <code>&lt;input type="file" multiple="multiple" name="files[]"&gt;</code> in my page. </p>
  
  <p>The following is my PHP for this:</p>
  
  <pre><code>session_start();
  include('connect.php');
  
  // Connect to server and select databse.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  $tbl_name="Pictures"; // Table name 
  
  if(isset($_POST['submit'])) {
      if(isset($_FILES['files'])){
      $errors= array();   
      $files = array();
  
      for($i=0; $i&lt;count($_FILES['files']['name']); $i++) {
          $tmpFilePath = $_FILES['files']['tmp_name'][$i];
          $file_name = $i.$_FILES['files']['name'][$i];
          $file_size =$_FILES['files']['size'][$i];
          $file_tmp =$_FILES['files']['tmp_name'][$i];
          $file_type=$_FILES['files']['type'][$i];    
  
          $desired_dir="images";
          $random_digit=rand(0000,9999);
  
          if ($tmpFilePath != ""){
              $newFilePath = "images/" . $random_digit . $_FILES['files']['name'][$i];
          }
  
          if($file_size &gt; 2097152){
              $errors[]='File size must be less than 2 MB';
          } else {
              move_uploaded_file($tmpFilePath, $newFilePath);
          }
  
          $files = array($newFilePath);
          $_SESSION['files'] = $files;
      }
      }
  }
  </code></pre>
  
  <p>So what this is doing currently is adding the files to my directory which is working fine, I have made it so that there is a random number before the files to avoid duplicates, because there will be a lot of files uploaded. It is uploading the multiple files fine but what I would like to do next is to go to a next page which will print out a number of text input fields based on how many files have been uploaded which allows the user to add captions to each file. Then, once submitted, each file and its caption will be inserted into the mySQL database.</p>
  
  <p>The easiest way I thought of doing this was to upload each $newFilePath (which is the file path of each image) into an array. I tried this and went to print the array using:</p>
  
  <pre><code>foreach($files as $f) {
  echo $f . '&lt;br&gt;';
  }
  </code></pre>
  
  <p>Which only came up with the first file uploaded and not all of them. I'd love to then add the array into a $_SESSION so that I can use them on another page but could probably do it all on the one page once I get it sorted. Any ideas?</p>