How to change or rename all file name in a folder in php?

If you want change or rename All file name or all images name in a folder so you can use this code and manipulate this code according to your need.

<?php
if ($handle = opendir('foldername1/')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
    echo $file;
   $newnameArr=explode('.',$file);// make sure in your file name there should not be .(full stop in File Name)
    $Change_imageName = "yourchangename".$newnameArr[1];   
    echo $Change_imageName.'<p></p>';
    rename( "foldername1/$file","foldername2/$Change_imageName");
    }
    closedir($handle);
} ?>

Note=> Where "Foldername1" is that folder where is images place now
And "Foldername2" is that folder where images will take place after rename or change the filename.

Foldername1 -- images should be in folder which you would like to rename
Foldername2 -- this folder must be blank or empty

Comments