首页 > 代码库 > php动态修改配置文件

php动态修改配置文件

 

技术分享

<?php
define("host","localhost");
define("port","3306");
define("user","root");
define("pwd","root2");
define("dbname","text23");

 

<?php
//1.读取配置文件的信息
$info = file_get_contents("dbcongfig.php");
$keyinfo = array("host"=>"主机","port"=>"端口","user"=>"账号","pwd"=>"密码","dbname"=>"数据库",);
//2.使用正则表达式解析配置文件的信息 .任意字符  * 任意数量 ?拒绝贪婪匹配
preg_match_all("/define\(\"(.*?)\",\"(.*?)\"\)/",$info,$temp);

echo "<h1>编辑配置文件</h1>";
echo "<form action=‘upt.php‘ method=‘post‘ >";
foreach($temp[1] as $k=>$v){
    echo "$keyinfo[$v]:<input type=‘text‘ name=‘{$v}‘ value=http://www.mamicode.com/‘{$temp[2][$k]}‘  /><br/><br/>";
}
echo "<input type=‘submit‘ value=http://www.mamicode.com/‘修改‘ />    ";
echo "<input type=‘reset‘ value=http://www.mamicode.com/‘重置‘ />";
echo "</form>";
<?php
//1.获取配置信息
$info = file_get_contents("dbcongfig.php");
//2.对配置文件进行正则替换
foreach($_POST as $k=>$v){
    $info = preg_replace("/define\(\"{$k}\",\".*?\"\)/","define(\"{$k}\",\"{$v}\")", $info);
}
file_put_contents("dbcongfig.php", $info);
echo "<script>alert(‘修改成功‘);</script>";
echo "<script>self.location=‘file_get_content.php‘</script>";

 

php动态修改配置文件