Retrieve all folder in a directories and change all folders' name that are in it
I want to retrieve all folders in a directory folder and change all the subfolder that in it. For example, in root folder Root, I want to change all subfolders A, B, C, D, .. to 1,2,34,... May I 开发者_如何转开发know how can I do it with php? Thanks.
<?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++;
}
}
?>
Of course, you will need to have proper CHMOD depending on where you run the script from.
Something like this:
$count = 0;
foreach(new DirectoryIterator('Root') as $fileInfo) {
if ($fileInfo->isDir() && !$fileInfo->isDot()) {
$count++;
rename($fileInfo->getPathName(), $fileInfo->getPath() . "/$count");
}
}
Links:
DirectoryIterator
rename
RecursiveDirectoryIterator
(if you with to do the same for sub-directories)
精彩评论