dqst96444 2018-11-30 10:33
浏览 76

PHP glob函数扫描目录

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 有没有帮写代码做实验仿真的
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥30 vmware exsi重置后登不上
    • ¥15 易盾点选的cb参数怎么解啊
    • ¥15 MATLAB运行显示错误,如何解决?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?