How does php modify the configuration file Config. php code as follows: & nbsp; & lt ;? Phpreturn & nbsp; array (& nbsp; 'version' & nbsp ;=& gt; & nbsp; '100 php: how to modify the configuration file
The Config. php code is as follows:
return array(
'version' => '20120823',
'secretKey' => '92fe5927095eaac53cd1aa3408da8135',
'areaname' => 'China',
);
Now I want to write a Common. php setConfig ($ fileName, $ value) method to modify the value of areaname. $ FileName is the name of the Config. php file, and $ value is the replaced value. I don't know how to write it ?? Php configuration file array
------ Solution --------------------
File_get_contents ()
File_put_contents ()
------ Solution --------------------
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');
However, due to the limitations of your solution, this function is not common.
For example, you cannot modify the secretKey.
It is recommended to rewrite
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');
------ Solution --------------------
There is a positive solution upstairs. good developers must have an idea on universality.