title: PHP - rename files in temp folder
body: <p>I am trying to rename all files I have in my temporal upload folder and instead give the files a unique ID reference number as filename followed by <code>_x</code> suffix that is supposed to increase after each rename, resulting in file names such as <code>myidnumber_1.jpg, myidnumber_2.jpg, etc.</code></p>
  
  <p>Problem is, my code doesnt seem to like the rename command and also does not keep the file extension. Any suggestions on how to address this?</p>
  
  <pre><code>// Get array of all files in temp folder and rename
  $check_folder = scandir("../../pages/fo_dmlog/attachments/".$_SESSION['Holidex']."/temp/".$_SESSION['myusername']."/");
  $n = 1;
  
  foreach ($check_folder as $check_file) {
      if (in_array($check_file, array(".",".."))) continue;
  
      $newName = str_replace($check_file,$logID."_".$n,$check_file);
      rename($check_folder . $check_file, $check_folder . $newName);
  
      echo "Attachment: $check_file&lt;br&gt;";
      $n++;
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>// Get array of all files in temp folder and rename
  $check_folder = scandir("../../pages/fo_dmlog/attachments/".$_SESSION['Holidex']."/temp/".$_SESSION['myusername']."/");
  $logID = "132456";
  $n = 1;
  
  foreach ($check_folder as $check_file) {
      if (in_array($check_file, array(".",".."))) continue;
  
      $extension = end(explode(".", $check_file));
      $newName = str_replace($check_file,$logID."_".$n.$extension,$check_file);
      rename($check_folder . $check_file, $check_folder . $newName);
  
      // instead of rename, can also move the files right away
      //move_uploaded_file($newName, "../".$logID."/" .$newName);
  
      echo "Attachment: $newName&lt;br&gt;";
      $n++;
  }
  </code></pre>