【发布时间】:2017-11-14 10:36:55
【问题描述】:
我想递归查找特殊路径中的所有文件和文件夹,我用这段代码来做
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