title: multiple files upload with only PHP and mysql
body: <p>I am trying to create a dynamic multiple file upload system where the user specifies the number of files to be uploaded. After the selection is made, the corresponding number of file upload fields are created and then the user can make the upload.</p>
  
  <p>Below is the script that I have. From my debugging the array is created successfully, but the upload fails. I will really appreciate some help if someone tells me what I am doing wrong.</p>
  
  <pre><code>&lt;form action="" method="post" enctype="application/x-www-form-urlencoded" name="max"&gt;
    &lt;table width="100%" border="0" cellspacing="2" cellpadding="2"&gt;
      &lt;tr&gt;
        &lt;td &gt;no of products&lt;/td&gt;
         &lt;td&gt;&lt;input name="max2" type="text" id="max2" size="3" maxlength="2" /&gt;
          &lt;/td&gt;
        &lt;td&gt;&lt;input type="submit" name="go" id="go" value="go&amp;gt;&amp;gt;" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/form&gt;
  &lt;form action="" method="post" enctype="multipart/form-data" name="uploader" id="uploader"&gt;&lt;table&gt;
    &lt;tr&gt;
  &lt;td&gt;
  
     &lt;?php
  global $max;
  if(isset($_POST['max2']))
      $max = $_POST['max2'];
  else
      $max = 3;
  
  for ($i = 1; $i &lt;= $max; $i++) {
  $forms = '&lt;table&gt;
            &lt;tr&gt;
              &lt;td&gt;File&lt;/td&gt;
              &lt;td&gt;&lt;label for="uploader"&gt;&lt;/label&gt;
                &lt;input name="uploader'.$i.'" type="file" id="uploader'.$i.'" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;';
  echo $forms.'&lt;br&gt;';//// creates a new form field depending on number of files specified by user
  }
  ?&gt;
   &lt;input name="max3" type="hidden" id="max" value="&lt;?php echo $_POST['max2'] ?&gt;" /&gt;
   &lt;?php
    if(isset($_POST['upload'])){
  
  $uploadArray= array();
  for ($i = 1;$i &lt;= $_POST['max3']; $i++) {
  $uploadArray[] = $_FILES['uploader'.$i]['name'];
          }
  print_r ($uploadArray); // display array to check it was properly created
  
  
   foreach($uploadArray as $file) {
  
      $target_path = "../Users/storename/upload/";
  
          if(file_exists($target_path) &amp;&amp; is_dir($target_path)){
  
                  if(move_uploaded_file($_FILES[$file]["tmp_name"], $target_path.'new'.$file)) {
                       echo "&lt;br&gt; The file ".  basename( $_FILES['$file']['name'])." has been uploaded";
                  } 
                  else{
                      echo "&lt;br&gt;The file ".$file." has NOT been uploaded";
                  }
  
      }
      else{
      echo 'invalid path&lt;br&gt;';
      echo $target_path;
      }
  }
  }
  
  ?&gt;&lt;/td&gt;
       &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;&lt;input type="submit" name="upload" id="upload" value="Submit" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
  
  &lt;/form&gt;
  </code></pre>