1

I am wondering if there is a way to recursively change file extensions from php within a directory and its subdirectories and subsubdirectories?

I have inherited a project that was on a windows server and somehow all pdf files have been uploaded with the extension .pdf.pdf Example filename.pdf.pdf
I am migrating the project to a LAMP server.

I would like to rename the files if possible to filename.pdf - They sit within directories and sub and subsubdirectories.

Is this something that is do-able at all? I am completely at a loss.

1 Answers1

1

Although somewhat messy, I was able to resolve my issue with the following code:

function listFolderFiles($dir){
  echo '<ul>';
  foreach (new DirectoryIterator($dir) as $fileInfo) {
    if (!$fileInfo->isDot()) {
      echo '<li>' . $fileInfo->getFilename();
      if (substr($fileInfo->getFilename(),-8) == ".pdf.pdf"){
         rename($fileInfo->getPathname(),substr($fileInfo->getPathname(),0,-4));
      }
      if ($fileInfo->isDir()) {
        listFolderFiles($fileInfo->getPathname());
      }
      echo '</li>';
    }
  }
  echo '</ul>';
}
listFolderFiles('job');

I hope this helps someone else sometime!