Search code examples
phpbatch-rename

PHP Rename all files using subdirectory as the new file name


How to rename this file below?

/Categories
|  /Celebrities
|  |  /Alyssa Milano
|  |  |  1.jpg
|  |  |  2.jpg
|  |  |  3.jpg
|  |  |  4.jpg
|  |  |  5.jpg
|  |  /Britney Spears
|  |  |  1.jpg
|  |  |  2.jpg
|  |  |  3.jpg
|  |  |  4.jpg
|  |  |  5.jpg
|  /Singers
|  |  /Rihanna
|  |  |  1.jpg
|  |  |  2.jpg
|  |  |  3.jpg
|  |  |  4.jpg
|  |  |  5.jpg
|  |  /Katy Perry
|  |  |  1.jpg
|  |  |  2.jpg
|  |  |  3.jpg
|  |  |  4.jpg
|  |  |  5.jpg

I want to rename 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg to

Alyssa Milano - 1.jpg, Alyssa Milano - 2.jpg, Alyssa Milano - 3.jpg

Britney Spears - 1.jpg, Britney Spears - 2.jpg, Britney Spears - 3.jpg

Rihanna - 1.jpg, Rihanna - 2.jpg, Rihanna - 3.jpg

Katy Perry - 1.jpg, Katy Perry - 2.jpg, Katy Perry - 3.jpg

Hi @GusDB,

I change the code to this below, but nothing happen

<?php
/**
 * Created by Gus de Boer
 * 28-10-2014
 * Stackoverflow
 */

function scanParentDir(){
    $categories = scandir('categories ');
    foreach($categories as $cat){
        if(file_exists('categories /'.$cat) && $cat != '.' && $cat != '..'){
            changesNamesToSubParentDir($cat);
        }
    }
}

function changesNamesToSubParentDir($cat){
    $albums = scandir('categories /'.$cat);
    foreach($albums as $album){
        if(file_exists('categories /'.$cat.'/'.$album) && $album != '.' && $album != '..'){
            changesNamesToParentDir($album);
        }
    }
}

function changesNamesToParentDir($album){
    $files = scandir('categories /'.$cat.'/'.$album);
    foreach($files as $file){
        if(file_exists('categories /'.$cat.'/'.$album.'/'.$file) && $file != '.' && $file != '..'){
            $filename = pathinfo('categories /'.$cat.'/'.$album.'/'.$file);
            rename('categories /'.$cat.'/'.$album.'/'.$file,     'categories /'.$cat.'/'.$album.'/'.$album.$filename['basename']);
        }
    }
}
?>

Thank you very much.


Solution

  • You can use scandir() (http://php.net/manual/en/function.scandir.php) to get an array of every directory in your "Celebrities" directory.

    scandir('path-to-directory');
    

    This will generate an array with all of the directories. After this you can loop through it, and do scandir() again for all of the images.

    You can rename the files with rename() (https://www.php.net/manual/en/function.rename.php)

    rename('path-to-file<oldname>', 'path-to-file<newname>');
    

    You should really try to code first and ask questions later. Also provide a code sample with what you have already tried.

    I hope this works out for you!

    EDIT:

    These functions will do the trick for you. This is just the basic renaming, you should add more checks and stuff.

    <?php
    /**
     * Created by Gus de Boer
     * 28-10-2014
     * Stackoverflow
     */
    
    function scanParentDir(){
        $directories = scandir('Categories/Celebrities');
        foreach($directories as $dir){
            if(file_exists('Categories/Celebrities/'.$dir) && $dir != '.' && $dir != '..'){
                changesNamesToParentDir($dir);
            }
        }
    }
    
    function changesNamesToParentDir($dir){
        $files = scandir('Categories/Celebrities/'.$dir);
        foreach($files as $file){
            if(file_exists('Categories/Celebrities/'.$dir.'/'.$file) && $file != '.' && $file != '..'){
                $filename = pathinfo('Categories/Celebrities/'.$dir.'/'.$file);
                rename('Categories/Celebrities/'.$dir.'/'.$file,     'Categories/Celebrities/'.$dir.'/'.$dir.$filename['basename']);
            }
        }
    }
    ?>