注:使用都有异步的方式修改,并不能第一时间获取到修改后的值,隔1秒请求接口就能获取到修改后的值了
第一种:
/**
* 修改扩展配置文件
* @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;
}
} //调用 $this->extraconfig(['test'=>'配置参数']);
$arr = [
'name' => 'extracofig',
'hello' => 'world'
];
Self::extraconfig($arr, 'testconf');
第二种:
public function setconfig($key,$value)
{
// $key = ['dasd'];// 更改的键值名称
// $value = [0,0];// 对应的值
if (is_array($key) and is_array($value)) {
for ($i = 0; $i < count($key); $i++) {
$keys[$i] = '/\'' . $key[$i] . '\'(.*?),/';
$values[$i] = "'". $key[$i]. "'". "=>" . "'".$value[$i] ."',";
}
$fileurl =$_SERVER['DOCUMENT_ROOT'] . "/../application/api/config.php";// 我的文件位置
$string = file_get_contents($fileurl); //加载配置文件
$string = preg_replace($keys, $values, $string); // 正则查找替换
print_r($string);
file_put_contents($fileurl, $string); // 写入配置文件
return true;
} else {
return false;
}
} //使用$this->setconfig(['test'],['配置参数']);