简体   繁体   中英

rename all subdirectories of directory in php

I have a directory structure like so:

.
├── uploads
|   ├── 1
|   |   ├── example.jpeg
|   |   └── example.jpeg
|   ├── 2
|   |   └── example.jpeg
|   ├── 3
|   |   ├── example.jpeg
|   |   ├── example.jpeg
|   |   └── example.jpeg

I wish to rename all the directories in my firectory uploads (in my example these are called 1 , 2 and 3 ). I want to rename all of these directories based on their current name. For example, I want 1 to become 1asd , 2 to 2asd and 3 to 3asd . I have looked for similar questions and found This one (which although the question seems similar is actually about something else) and this one , which is about renaming files. I tried:

if ($handle = opendir('../path/to/uploads')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = $fileName.'asd';
        rename($fileName, $newName);
    }
    closedir($handle);
}

This doesn't work because all $filename is always . . My guess is because it's about directories and not files. How do I target the directories instead?

side-note1 : the directories I wish to rename contain files which I do not want to lose/delete. In my example they are all called example.jpeg .

side-note2 : the path to my uploads directory is correct, I tested this.

Use glob and rename .

Make sure that you run this from the proper directory or change the path accordingly.

Works as it is if your PHP script is located on the parent directory of uploads

foreach (glob("uploads/[1|2|3]",GLOB_ONLYDIR) as $filename) {
    rename($filename, $filename."asd");
}

In case of multiple directories the above would change a bit to the following:

$directories = array_merge_recursive(glob("uploads/[0-9]*[0-9]",GLOB_ONLYDIR),glob("uploads/[0-9]",GLOB_ONLYDIR));

foreach ($directories as $directory) {
    rename($directory, $directory."asd");
}

glob("uploads/[0-9]*[0-9]",GLOB_ONLYDIR) matches anything starting from a number and ending to one.

glob("uploads/[0-9]",GLOB_ONLYDIR) matches any directory that has a single digit number for name.

You want to check to make sure it is a directory and check that it is not the current directory . or the parent .. directory:

if(is_dir($fileName) && $fileName!= "." && $fileName!= "..") {
   $newName = $fileName.'asd';
   rename($fileName, $newName);
}

Also, as mcserep states There is a syntax error in $newName = $fileName.'asd'); , note the closing parentheses.

After some trial and error I managed to figure out a solution:

$dir = '../path/to/uploads/';

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($filename = readdir($dh)) !== false) {
                    if($filename!= "." && $filename!= "..") {
                        rename($dir.$filename, $dir.$filename."asd");
                    }
        }
        closedir($dh);
    }
}

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