0

I have 2500 images in a Folder, which has NAME word in all the images. For examples

Peter Wang B5357550.jpg
Sander Mackiney B5355624.jpg

what i need to do is read all the filenames and rename it to the following

B5357550.jpg
B5355624.jpg

So remove NAME and SURNAME from filename, is it possible in PHP to do bulk renaming ? (All student IDs are in format of Bxxxxxxx)

2
  • 1
    Yes, it is possible to do bulk renaming in PHP. Is there anything you have tried that we can help with?
    – Jim
    Commented Oct 14, 2014 at 15:51
  • On Linux you may consider rename command to bulk rename files. You can find some examples at tips.webdesign10.com/…
    – softius
    Commented Oct 14, 2014 at 17:12

2 Answers 2

2

Quick, simple solution:

$dir = $_SERVER['DOCUMENT_ROOT'].'/your-folder-to-files';
$files = scandir($dir);
unset($files[0],$files[1]);
foreach ($files as $oldname){
    $newname = substr($oldname, -12);
    rename ($dir.'/'.$oldname, $dir.'/'.$newname);
}

N.B.: You may need to change the server path to something similar to:

$dir = "/home/users/you/folder_files/";

or

$dir = "folder_files/";
  • If $_SERVER['DOCUMENT_ROOT'] does not work for you.
0
-1

If they're all in that format, it would be simple to fix, yes. Run glob to get all the .jpg files into an array, then simply explode the filename on spaces, use a foreach loop on that array, use end to get the last section, and rename the file to that string.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.