title: Using php to rename all files in folder
body: <p>new php programmer here. I have been trying to rename all the files in a folder by replacing the extension.</p>
  
  <p>The code I'm using is from <a href="https://stackoverflow.com/questions/4993590/bulk-rename-files-in-a-folder-php">the answer to a similar question on SO.</a></p>
  
  <pre><code>if ($handle = opendir('/public_html/testfolder/')) {
  while (false !== ($fileName = readdir($handle))) {
      $newName = str_replace(".php",".html",$fileName);
      rename($fileName, $newName);
  }
  closedir($handle);
  </code></pre>
  
  <p>}</p>
  
  <p>I get no errors when running the code, but no changes are made to the filenames.</p>
  
  <p>Any insight on why this isn't working? My permission settings should allow it.</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT: I get a blank page when checking the return value of rename(), now trying something with glob() which might be a better option than opendir...?</p>
  
  <p>EDIT 2: With the 2nd code snippet below, I can print the contents of $newfiles. So the array exists, but the str_replace + rename() snippet fails to change the filename.</p>
  
  <pre><code>$files = glob('testfolder/*');
  
  
  foreach($files as $newfiles) 
      {
  
      //This code doesn't work:
  
              $change = str_replace('php','html',$newfiles);
      rename($newfiles,$change);
  
             // But printing $newfiles works fine
             print_r($newfiles);
  }
  </code></pre>