Here is a PHP function that allows for batch renaming of files within a specified folder:
This function accepts three parameters:
$dirSpecify the directory path for batch renaming of files.$searchThe string to search for and replace.$replaceThe string to be replaced.
The function is used. scandir The function retrieves all files within a specified directory, then checks each file; if the file name contains the search string, it constructs a new file name and performs a renaming operation. During this renaming process, the function also checks whether the new file name already exists to avoid naming conflicts.
You can call this function using the following methods:
batchRenameFiles('/path/to/directory', 'old_string', 'new_string');among,/path/to/directory The directory path for batch renaming.old_string The string to search for and replace,new_string The string to be replaced.
Successful rendering result:

You can tweak the output a bit – just add a <br> tag.