如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
/**
* 修改扩展配置文件
* @param array $arr 需要更新或添加的配置
* @param string $file 配置文件名(不需要后辍)
* @return bool
*/
function extraconfig($arr = [], $file = 'extraconfig')
{
if (is_array($arr)) {
$filename = $file . EXT;
$filepath = APP_PATH . 'extra/' . $filename;
if (!file_exists($filepath)) {
$conf = "<?php return [];";
file_put_contents($filepath, $conf);
}
$conf = include $filepath;
foreach ($arr as $key => $value) {
$conf[$key] = $value;
}
$time = date('Y/m/d H:i:s');
$str = "<?php\r\n/**\r\n * 由extraconfig建立.\r\n * $time\r\n */\r\nreturn [\r\n";
foreach ($conf as $key => $value) {
$str .= "\t'$key' => '$value',";
$str .= "\r\n";
}
$str .= '];';
file_put_contents($filepath, $str);
return true;
} else {
return false;
}
}
$arr = [
'name' => 'extracofig',
'hello' => 'world'
];
app/extra/web.php
<?php
return [
'name' => 'extracofig',
'hello' => 'world'
]
王明昌博客
