欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP文件操作之修改配置文件

程序员文章站 2022-04-08 23:19:39
...

PHP文件操作之修改配置文件


一、引导页


<?php
include("dbconfig.php");
?>

<form action="edit.php" method="post">
    <br />
    端口:<input type="text" name="DB_HOST" value="<?php echo DB_HOST;?>" /><br />
    用户名:<input type="text" name="DB_USER" value="<?php echo DB_USER;?>" /><br />
    密码:<input type="text" name="DB_PASS" value="<?php echo DB_PASS;?>" /><br />
    数据库名:<input type="text" name="DB_NAME" value="<?php echo DB_NAME;?>" /><br />

    <input type="submit" value="修改" />
</form>

二、修改页面页面


<?php
//获取配置文件的内容
$string = file_get_contents('config.php');

foreach ($_POST as $key=>$value) {
    //正则匹配对应的一行
    $old_inf = "/define\('$key','.*?'\);/";
    //要修改成的数据
    $new_inf = "define('$key','$value');";
    //替换
    $string = preg_replace($old_inf,$new_inf,$string);
}
//写入配置文件
file_put_contents('config.php',$string);

echo "修改成功";
相关标签: 文件