郑锋

我就是我

php批量更改文件内容

使用这个代码,可以将某个文件夹内的所有特定文件中的内容改为其它内容。

$scanDir = '/home/wwwroot/default/zb_users/theme/video'; //文件夹
$ext     = array('php','js','css','txt','xml');  //需要更改的文件后缀
$text    = 'dedevv';  //要更改的字符
$newText = 'newstr';   //新字符

function dirfolder($dir){
    global $ext,$text,$newText;
    
    foreach (scandir($dir) as $value) {
        if ($value != '.' && $value != '..'){
            $path = $dir.'/'.$value;
            if (is_dir($path)){
                dirfolder($path);
            }else{
                if (preg_match('/\.('.implode('|',$ext).')$/',$path)){
                    $str = file_get_contents($path);
                    if (strpos($str,$text) !== false){
                        $str = str_replace($text,$newText,$str);
                        file_put_contents($path,$str);
                    }
                }
            }
        }
    }
    
}

dirfolder($scanDir);


«    2024年1月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
网站收藏
友情链接
  • RainbowSoft Studio Z-Blog
  • 订阅本站的 RSS 2.0 新闻聚合