我需要帮助。我想以递归方式将所有文件重命名为目录。我有一个代码来测试,但它只在该文件夹中重命名而不是递归。如何以递归方式完成它。
这是我使用的代码
<?php
$directory="/data";
$files = scandir($directory);
foreach($files as $key=>$name){
$oldName = $name;
$newName = strtolower($name);
rename("$directory/$oldName","$directory/$newName");
}
?>
答案 0 :(得分:12)
您可以使用SPL的RecursiveDirectoryIterator。
<?php
$path = realpath('your/path/here');
$di = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach($di as $name => $fio) {
$newname = $fio->getPath() . DIRECTORY_SEPARATOR . strtolower( $fio->getFilename() );
echo $newname, "\r\n";
//rename($name, $newname); - first check the output, then remove the comment...
}