Php 重命名路径中的所有文件和文件夹

Php 重命名路径中的所有文件和文件夹,php,Php,我想递归地找到特殊路径中的所有文件和文件夹,我用这段代码来完成 public static function getDirContents($dir, &$results = array()){ $files = scandir($dir); foreach($files as $key => $value){ $path = realpath($dir.DIRECTORY_SEPARATOR.$value); if(!is_dir

我想递归地找到特殊路径中的所有文件和文件夹,我用这段代码来完成

public static function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            self::getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}
$files = FileHelper::getDirContents($path_from);
                if (isset($files)) {
                    $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
                    foreach ($files as $file) {
                        $newName = str_replace($replacers, "_", $file);
                        if ($newName != $file) {
                            Logger::setLog('renaming', "Renaming: $file to $newName");
                            rename($file, $newName);
                        }
                    }
                }
我想用空格,下划线来代替名字 所以我用这个代码

public static function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            self::getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}
$files = FileHelper::getDirContents($path_from);
                if (isset($files)) {
                    $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
                    foreach ($files as $file) {
                        $newName = str_replace($replacers, "_", $file);
                        if ($newName != $file) {
                            Logger::setLog('renaming', "Renaming: $file to $newName");
                            rename($file, $newName);
                        }
                    }
                }
但问题发生在我重命名父文件夹时,因为路径已更改或丢失,所以系统无法重命名子文件夹和文件
那么我该如何解决我的问题呢?

以下是递归重命名文件和文件夹的工作代码

<?php
    renameFiles('C:\assets\images\\','doctor'); // target path is C:\assets\images\doctor 
    function renameFiles($base,$path){
      $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
      foreach (new DirectoryIterator($base.$path) as $object) {
          if($object->isDot()) continue;
          if(is_dir($base.$path.'\\'.$object->getFilename())){
            renameFiles($base,$path.'\\'.$object->getFilename());
          }
          $oldName = $object->getFilename();
          $newName = str_replace($replacers, "_", $oldName);
          $oldFullName = $object->getPath() . DIRECTORY_SEPARATOR . $oldName;
          $newFullName = $object->getPath() . DIRECTORY_SEPARATOR . $newName;
          rename($oldFullName, $newFullName);
          echo "old : $oldName , new: $newName <br/>\n";
          //echo $object->getFilename() . "<br>\n";
      }
    }
?>

以下是递归重命名文件和文件夹的工作代码

<?php
    renameFiles('C:\assets\images\\','doctor'); // target path is C:\assets\images\doctor 
    function renameFiles($base,$path){
      $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
      foreach (new DirectoryIterator($base.$path) as $object) {
          if($object->isDot()) continue;
          if(is_dir($base.$path.'\\'.$object->getFilename())){
            renameFiles($base,$path.'\\'.$object->getFilename());
          }
          $oldName = $object->getFilename();
          $newName = str_replace($replacers, "_", $oldName);
          $oldFullName = $object->getPath() . DIRECTORY_SEPARATOR . $oldName;
          $newFullName = $object->getPath() . DIRECTORY_SEPARATOR . $newName;
          rename($oldFullName, $newFullName);
          echo "old : $oldName , new: $newName <br/>\n";
          //echo $object->getFilename() . "<br>\n";
      }
    }
?>

首先重命名文件名,然后才重命名其父目录。从底部开始…:)首先重命名文件名,然后才重命名其父目录。从底部开始…:)