简体   繁体   中英

PHP: rename all files to lower case in a directory recursively

I need help. I want to rename all files to lower case within a directory recursively. I have a code to test but it only rename within that folder not recursively. How can I make it to do it recursively.

This is the code I use

<?php
 $directory="/data";
 $files = scandir($directory);
 foreach($files as $key=>$name){
    $oldName = $name;
    $newName = strtolower($name);
    rename("$directory/$oldName","$directory/$newName");
  }
?>

You can use the SPL's RecursiveDirectoryIterator for that.

<?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...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM