thinkphp5一键清除缓存

入口文件定义缓存文件路径常量

define('DS', DIRECTORY_SEPARATOR);
defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS);
defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
defined('EXTEND_PATH') or define('EXTEND_PATH', ROOT_PATH . 'extend' . DS);
defined('VENDOR_PATH') or define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS);
defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DS);
defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH . 'log' . DS);
defined('CACHE_PATH') or define('CACHE_PATH', RUNTIME_PATH . 'cache' . DS); //缓存地址 cache
defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS);

 

//清除缓存
    public function clearCache()
    {
        //echo CACHE_PATH;  //缓存地址 --CACHE_PATH
        if(file_exists(CACHE_PATH)){
            $this->delDir(CACHE_PATH);
            operate_log(session('user_id'),'admin/index/clearCache','清除站点缓存',get_client_ip(),0);
            $this->success('清除成功','admin/index/index');
        }
    }


    //删除缓存文件
    public function delDir($dirName) {
        $dh = opendir($dirName);
        //循环读取文件
        while ($file = readdir($dh)) {
            if($file != '.' && $file != '..') {
                $fullpath = $dirName . '/' . $file;
                //判断是否为目录
                if(!is_dir($fullpath)) {
                    //如果不是,删除该文件
                    if(!unlink($fullpath)) {
                        echo $fullpath . '无法删除,可能是没有权限!<br>';
                    }
                } else {
                    //如果是目录,递归本身删除下级目录
                    $this->delDir($fullpath);
                }
            }
        }
        //关闭目录
        closedir($dh);
        //删除目录
        //if(!rmdir($dirName)) {
        //     R('Public/errjson',array($dirName.'__目录删除失败'));
        //}
    }

 

 

 

posted @ 2019-06-30 10:11  搬砖小伙子  阅读(3403)  评论(0编辑  收藏  举报