野生大熊猫

discuz开发插件写入文件缓存
开发插件写入文件缓存一、函数$script 文件名$cachedata 文件内容$prefix 文件前缀funct...
扫描右侧二维码阅读全文
02
2018/03

discuz开发插件写入文件缓存

开发插件写入文件缓存

一、函数

$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, "<?php\n//Discuz! cache file, DO NOT modify me!\n//Identify: ".md5($prefix.$script.'.php'.$cachedata.$_G['config']['security']['authkey'])."\n\n$cachedata?>");
        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);

三、效果

在文件夹data\sysdata\中会多出一个cache_dzl8_rurl.php的文件,并且文件的内容为:

<?php
//Discuz! cache file, DO NOT modify me!
//Identify: 3f968d7f2c1b795d388ed9270be1b907

$contents=array (
  'username' => 'test',
  'uid' => 123,
);
$dateline=1519979117;
?>
Last modification:March 2nd, 2018 at 04:34 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment