Bulk Rename Files in a Folder – PHP

Yeah, just open the directory and create a loop to access all images and rename them, like:

<?php

if ($handle = opendir('./path/to/files')) {
    while (false !== ($fileName = readdir($handle))) {
      if($fileName != '.' && $fileName != '..') {
         $newName = str_replace("SKU#","",$fileName);
         rename('./path/to/files/'.$fileName, './path/to/files'.$newName);
      }
    }
    closedir($handle);
}
?>

References:

http://php.net/manual/en/function.rename.php

http://php.net/manual/en/function.readdir.php

http://php.net/manual/en/function.str-replace.php

Leave a Comment