检索目录中的所有文件夹并更改所有文件夹'里面的名字


Retrieve all folder in a directories and change all folders' name that are in it

我想检索目录文件夹中的所有文件夹并更改其中的所有子文件夹。例如,在根文件夹根,我想改变所有子文件夹A, B, C, D,…1, 2, 34岁……我可以知道我怎么用php做吗?谢谢。

<?php
$basedir = "/tmp"; //or whatever your "to change" home directory is
$contents = scandir($basedir);
$count = 1;
foreach ($contents as $check) {
    if (is_dir($basedir . "/" . $check) && $check != "." && $check != "..") {
        rename($basedir . "/" . $check, $basedir . "/" . $count);
        $count++;
    }
}
?>

当然,你需要有合适的CHMOD,这取决于你从哪里运行脚本。

像这样:

$count = 0;
foreach(new DirectoryIterator('Root') as $fileInfo) {
    if ($fileInfo->isDir() && !$fileInfo->isDot()) {
        $count++;
        rename($fileInfo->getPathName(), $fileInfo->getPath() . "/$count");
    }
}


链接:

  • DirectoryIterator
  • rename
  • RecursiveDirectoryIterator(如果你想对子目录做同样的事)