php如何修改配置文件
Config.php代码如下:
<?php
return array(
'version' => '20120823',
'secretKey' => '92fe5927095eaac53cd1aa3408da8135',
'areaname' => 'China',
);
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, '<?php return ' . var_export($a, 1) . ';');
}
setConfig('config.php', 'aaa');
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, '<?php return ' . var_export($a, 1) . ';');
}
setConfig('areaname', 'bbb');