php批量更改文件内容
文章标签:
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);
登录后可发表评论
点击登录