I have a stupid question but a clarification will help me to debug a script that I'm writing.
I'm using the php glob()
function to scan files inside some different directories. The script I've made is supposed to run and scan these directory then if the given pattern find a matche with the names of the files inside the folder, a function will bulk rename these files. My question is, I need to rescan the folder after the files are renamed or I can use only one var to scan the files and then rename them avoiding to rescan the directory?
Here is a sample of the code:
define('ADMIN_PATH', ABSPATH.'/admin');
// the files inside admin are prefixed with the admin_filename.php prefix
$admin_root_files = glob(ADMIN_PATH.'/*.php');
// with this function I'm removing the prefix
function prefix_remover(array $files, $pattern, $replace){
foreach($files as $_file){
$prefix_replace = preg_replace("/{$pattern}/", "{$replace}", $_file);
$unprefix_file = rename($_file, $prefix_replace);
}
return true;
}
If i call the prefix remover function twice (I need to open the renamed files and then replace the prefix inside the code in bulk), the console will log the php error that usually is referred to existence of a file. I think that it's related to the fact that the files are renamed, but I'm not sure about.