PHP: rename all files to lower case in a directory recursively
我需要帮助。我想递归地将目录中的所有文件重命名为小写。我有一个代码要测试,但它只在那个文件夹中重命名,而不是递归地。我怎样才能使它递归地执行。
这是我用的代码
1 2 3 4 5 6 7 8 9 | <?php $directory="/data"; $files = scandir($directory); foreach($files as $key=>$name){ $oldName = $name; $newName = strtolower($name); rename("$directory/$oldName","$directory/$newName"); } ?> |
您可以使用SPL的递归DirectoryIterator。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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," "; //rename($name, $newname); - first check the output, then remove the comment... } |