本教程是非常经典的PHP同步修改配置文件示例教程,非常适合新手朋友学习。
主要学习了file_get_contents函数的读写方法,
file_get_contents函数读取文件的方法,示例:$info=file_get_contents("文件路径");
file_put_contents函数写入内容的方法,示例:file_put_contents("文件路径",写入内容变量);
文件结构:
index.php 主页,显示和表单提交
config 配置文件,储存数据
doUpdate.php 修改处理
index.php代码:
修改配置 $v){ echo ""; echo "{$v}"; echo ""; echo ""; } ?>
config.php代码:
doUpdate.php代码:
$v){ //正则替换 $info=preg_replace("/define("{$k}",".*?")/","define("{$k}","{$v}")",$info); } //回填 file_put_contents("config.php",$info); echo "ok"; header("refresh:1;url=index.php"); ?>
以上案列用到了正则匹配的方法修改指定内容,不会正则的可以使用<<<EOF 和 EOF的方法直接修改整个文件的内容。
$newdata = <<<php <?php 'www' = '51yma.cn'; 'm' = '51yma.cn'; php;