title: How do I use php to upload and rename multiple files using a dropdown menu for the file names?
body: <p>I need to allow users to upload multiple files and then choose the filename using a drop-down menu.  The files will be used for my schools 'options' website and the files need to be named using course codes.  Unfortunately, users often misname so I want to allow them to upload files and select the course.  The plan is to automatically rename them so that the code is correct.  Users select their name and then upload files so that their uploads go in a dedicated folder.</p>
  
  <p>I can do it for one file but I need to be able to do it for more than one file.  I can upload multiple files but can't seem to rename them.  The problem is that I don't know how to link multiple new file names to the array that can be created using $_FILES</p>
  
  <p>Here's the code for uploading and renaming a single file (it uses .jpg files but will be changed to .pdf files in due course) ...</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php 
  
  // Makes directory and changes file name for one file
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
   
   $uploads = $_POST['name'];
   if (!is_dir($uploads))
   {
    mkdir($uploads);
   }
   
   $info = pathinfo($_FILES['userFile']['name']);
   $ext = $info['extension']; // get the extension of the file
   $newname = $_POST['new_name'].".".$ext; 
  
   $target = $uploads.'/'.$newname;
   move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
   
  }
  ?&gt;
  
  &lt;form name="UploadForm" enctype="multipart/form-data" method="post"&gt;
      
      &lt;p&gt;
      Find the file(s) you want to upload and click the "Upload" button below.
   &lt;/p&gt;
   &lt;p&gt;
   &lt;b&gt;Teacher Code:&lt;/b&gt; 
    &lt;select name="name"&gt;
     &lt;option value="Mr_A"&gt;Mr A&lt;/option&gt;
     &lt;option value="Mr_B"&gt;Mr_B&lt;/option&gt;
     &lt;option value="Mr_C"&gt;Mr C&lt;/option&gt;
    &lt;/select&gt;
   &lt;/p&gt;
   
   &lt;p&gt;Select the course from the box and then select the .pdf outline...&lt;/p&gt;
  
      
    &lt;p&gt;
    
    &lt;select name="new_name"&gt;
     &lt;option value=""&gt;Choose...&lt;/option&gt;
     &lt;option value="L1PROG01"&gt;Programming - Level 1&lt;/option&gt;
     &lt;option value="L2PROG01"&gt;Programming - Level 2&lt;/option&gt;
     &lt;option value="L3PROG01"&gt;Programming - Level 3&lt;/option&gt;
     &lt;option value="L1WEBD01"&gt;Web Design - Level 1&lt;/option&gt;
     &lt;option value="L2WEBD01"&gt;Web Design - Level 2&lt;/option&gt;
     &lt;option value="L3WEBD01"&gt;Web Design - Level 3&lt;/option&gt;
    &lt;/select&gt;
    
    &lt;input name="userFile" type="file" /&gt;
    
    &lt;/p&gt;
    
      &lt;br/&gt;Check this box &lt;input autocomplete="off" type="checkbox" checked name="overwrite" /&gt; to &lt;strong&gt;overwrite&lt;/strong&gt; existing files.
      &lt;input type="submit" value="Upload" /&gt;
      &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>