Heim >Backend-Entwicklung >PHP-Tutorial >php如何修改配置文件
Config.php代码如下:
<?phpreturn array( 'version' => '20120823', 'secretKey' => '92fe5927095eaac53cd1aa3408da8135', 'areaname' => 'China',);
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, '<?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');
楼上正解,好的开发者一定在通用性上有想法