開發外掛寫入檔案快取

一、函式

$script 檔名
$cachedata 檔案內容
$prefix 檔案字首

function writetocache($script, $cachedata, $prefix = 'cache_') {
    global $_G;

    $dir = DISCUZ_ROOT.'./data/sysdata/';
    if(!is_dir($dir)) {
        dmkdir($dir, 0777);
    }
    if($fp = @fopen("$dir$prefix$script.php", 'wb')) {
        fwrite($fp, "");
        fclose($fp);
    } else {
        exit('Can not write to cache files, please check directory ./data/ and ./data/sysdata/ .');
    }
}

二、用法

global $_G;
//引入檔案
require_once 'source/function/function_cache.php';
@include_once 'data/sysdata/cache_dzl8_rurl.php';
//儲存內容
$contents['username']="test";
$contents['uid']=123;
$dateline=TIMESTAMP;
//整理
$cacheArray = "$contents=".arrayeval($contents).";n";
$cacheArray .= "$dateline=$dateline;n";
//寫入
writetocache('dzl8_rurl', $cacheArray);

三、效果

在資料夾 datasysdata 中會多出一個 cache_dzl8_rurl.php 的檔案,並且檔案的內容為:

 'test',
  'uid' => 123,
);
$dateline=1519979117;
?>