您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 安庆分类信息网,免费分类信息发布

php怎么修改配置文件

2025/10/18 21:00:42发布42次查看
php如何修改配置文件
config.php代码如下:
'20120823',
'secretkey' => '92fe5927095eaac53cd1aa3408da8135',
'areaname' => 'china',
);

现在想写个common.php定义setconfig($filename, $value)方法,去修改areaname的值。其中$filename为config.php文件名称, $value为更换的值,不知道具体怎么写?? php 配置文件 数组
------解决方案--------------------
file_get_contents()
file_put_contents()
------解决方案--------------------
function setconfig($filename, $value) {
ob_start();
$a = @include($filename);
ob_end_clean();
if(! is_array($a)) trigger_error(invalid data file, e_user_error);
$a['areaname'] = $value;
file_put_contents($filename, ' }
setconfig('config.php', 'aaa');
但是由于你的方案的限制,这个函数有着不通用的毛病
比如要想修改 secretkey 就不可以了
建议改写做function setconfig($key, $value, $filename='config.php') {
ob_start();
$a = @include($filename);
ob_end_clean();
if(! is_array($a)) trigger_error(invalid data file, e_user_error);
$a[$key] = $value;
file_put_contents($filename, ' }
setconfig('areaname', 'bbb');
------解决方案--------------------
楼上正解,好的开发者一定在通用性上有想法
安庆分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product