天天看點

php函數修改配置檔案,ThinkPHP5.0 修改配置檔案函數。

TP5 修改配置檔案函數

function setconfig($file,$data)

{

if (is_array($data)){

$fileurl = APP_PATH .$file.".php";

$string = file_get_contents($fileurl); //加載配置檔案

foreach ($data as $key => $value) {

$pats = '/\'' . $key . '\'(.*?)\',/';

$reps = "'". $key. "'". "=> " . "'".$value ."',";

$string = preg_replace($pats, $reps, $string); // 正則查找然後替換

}

file_put_contents($fileurl, $string); // 寫入配置檔案

return true;

}else{

return false;

}

}

此函數說明:

不會影響配置檔案的注釋或換行說明等内容

配置檔案可以是擴充配置檔案或資料庫配置檔案或TP5核心配置檔案。

修改方式為數組方式,友善在控制器組裝修改參數

控制器中調用方式:

$value = [

'app_status' => $data['app_status'],

'closing' => $data['closing']

];

setconfig('extra/appset',$value);

配置檔案代碼示例:

return [

// +----------------------------------------------------------------------

// | 基礎設定

// +----------------------------------------------------------------------

// 系統狀态

'app_status'=> '1',

// 關閉提示

'closing'=> '系統更新中哦...',

];