直接给你代码:
<?php
function replaceStringInTxt($path)
{
$path = ltrim($path, '/') . '/';
$files = scandir($path);
if ($files) {
foreach($files as $file) {
if ($file == '.' || $file == '..' ){
continue;
}
$newPath = $path. $file;
if (is_dir($newPath) {
replaceStringInTxt($newPath);
}else{
//开始替换
$content = file_get_contents($newPath);
$content = str_replace("system****config", "config", $content);
file_put_contents($newPath, $content);
}
}
}
}
//目录的地址
$path = "C://aa";
//执行
replaceStringInTxt($path);
By the way, 代码未经检验,请自行 debug , 随便写的。
追问你好,我的系统是 linux系统,测试不行!
追答ER, 这代码也适合 linux 的,贴一下你实际运行的代码来看看。
追问我的php运行环境是linux系统,此代码放至服务端,在本地浏览器运行后,无任何反应,空白,但txt中的内容没有改变,php运行环境在windows系统中,此代码能正常运行,基本能实现功能!linux系统是不是跟路径有关系?请问大侠!
追答应该和路径有关系,你服务器的路径是什么?