Search code examples
phpfilerenamebulk

How can I bulk rename files in a photo folder with sequential numbers


This was a poorly worded question

My main goal was to display many images or even a range from a set arbitrarily, renaming the files to sequential numbers seemed like it would make the displaying or iterating through the files easier if they just differed by 1 rather than random strings.

-- anyway I'm going to read... seems glob is according to php manual ... The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function

The files have random names but they are all .jpg's

As an example, "this name".jpg is replaced with "i+1".jpg

So that I can display the photos lazily using a for loop incrementing the numbers. The primary purpose is to display the photos regardless of their file names.


Solution

  • You can use php glob and rename functions:

    $num = 1;
    foreach (glob("/path/to/images/*.jpg") as $filename) {
        $fileNoExtension = basename($filename, ".jpg");
        rename ($filename, "$fileNoExtension{$num}.jpg");
        $num++;
    }