
在php中怎样修改配置文件?
在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 "修改成功";推荐教程:《PHP视频教程》
立即学习“PHP免费学习笔记(深入)”;
以上就是在php中怎样修改配置文件?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号