title: Validating Multiple Dynamically Created Fields
body: <p>I have a form with dynamically created fields one of which is a file input field.</p>
  
  <p>For non-dynamically created input fields, I use this to validate on the server side: </p>
  
  <pre><code>if(isset ($_POST['submitform']))
      {
          $error = array();
  
          foreach($_POST as $key =&gt; $value) 
              {
                  $data[$key] = filter($value);
              }
  
          if(empty($data['randominputfield']))
              {
                  $error[] = "&lt;p&gt;Input something here&lt;/p&gt;";
              }
  
          $target = "path/to/folder/"; 
  
          $fileinput = ($_FILES['fileupload']['name']);
          $filebasename = substr($fileinput, 0, strripos($fileinput, '.')); 
          $fileextension  = substr($fileinput, strripos($fileinput, '.'));
          $filesize = $_FILES["fileupload"]["size"];
          $fileformats = array('.png','.jpeg','.jpg','.gif');
  
          if (in_array($fileextension, $fileformats) &amp;&amp; ($filesize &lt;= 350000))
              {
                  // Rename file
                  $fileinputnewname = md5($filebasename) . $fileextension;
                  if (file_exists("path/to/folder/" . $fileinputnewname))
                      {
                          $error[] = "&lt;p&gt;A file with this name already exists&lt;/p&gt;";
                      }
                  else
                      {
                          $filemove = move_uploaded_file($_FILES["fileupload"]["tmp_name"], "path/to/folder" . $fileinputnewname);
                      }
              }
  
          elseif (empty($filebasename))
              {
                  $error[] = "&lt;p&gt;Please select a file to upload&lt;/p&gt;";
              }
  
          elseif ($filesize &gt; 350000)
              {
                  $error[] = "&lt;p&gt;Files should not exceed 350kB&lt;/p&gt;";
              }
          else
              {
                  $error[] = "&lt;p&gt;Only files with the extensions ". implode(', ',$fileformats)." are allowed&lt;/p&gt;"; 
                  unlink($_FILES["fileupload"]["tmp_name"]);
              } 
  
  
          if(empty($error))
              { 
                  //perform database insertion
              }
      }
  </code></pre>
  
  <p>and that works beautifully. </p>
  
  <p>But now I want to give the user the opportunity of adding an infinite number of fields (I accomplished this using jQuery) so that they can upload more than one set of data (i.e. the random input and a file) at the same time and I want to validate and then proceed to store each set of data in the database. Any ideas? </p>