在基类控制器里,写上
clear()
方法,到时候用于调用,在也不是JS
里alert('清除缓存了')
当然,5.1取消了CACHE_PATH
TEMP_PATH
常量,所以要定义这些缓存目录,还需引入use think\facade\Env
// 我是直接按照文档,设置了2个变量来存储缓存目录,当然还可以在配置文件中进行配置,然后通过config获取
public function clear(){
$CACHE_PATH = Env::get('runtime_path') . 'cache/';
$TEMP_PATH = Env::get('runtime_path'). 'temp/';
if (delete_dir_file($CACHE_PATH) && delete_dir_file($TEMP_PATH)) {
return json(["code"=>1,"msg"=>"清除缓存成功"]);
} else {
return json(["code"=>0,"msg"=>"清除缓存失败"]);
}
}
这个时候发现
delete_dir_file()
函数未定义,我们去公共函数里进行定义并写出来:
由于5.1 没有DS
常量了,所以选用DIRECTORY_SEPARATOR
来代替
common.php:
/**
* 循环删除目录和文件
* @param string $dir_name
* @return bool
*/
function delete_dir_file($dir_name) {
$result = false;
if(is_dir($dir_name)){
if ($handle = opendir($dir_name)) {
while (false !== ($item = readdir($handle))) {
if ($item != '.' && $item != '..') {
if (is_dir($dir_name . DIRECTORY_SEPARATOR. $item)) {
delete_dir_file($dir_name . DIRECTORY_SEPARATOR. $item);
} else {
unlink($dir_name . DIRECTORY_SEPARATOR. $item);
}
}
}
closedir($handle);
if (rmdir($dir_name)) {
$result = true;
}
}
}
return $result;
}
public function delCache() {
if (Dir::del(Env::get('runtime_path').'cache/') {
$data = ['status' => 1, 'info' => '缓存删除成功'];
} else {
$data = ['status' => 0, 'info' => '缓存删除失败];
}
return json($data);
}
这个需要一个
Dir类
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: superman <953369865@qq.com>
// +----------------------------------------------------------------------
namespace Org\Util;
use Think\Db;
final class Dir
{
/**
* @param string $dir_name 目录名
* @return mixed|string
*/
static public function dirPath($dir_name)
{
$dirname = str_ireplace("\\", "/", $dir_name);
return substr($dirname, "-1") == "/" ? $dirname : $dirname . "/";
}
/**
* 获得扩展名
* @param string $file 文件名
* @return string
*/
static public function getExt($file)
{
return strtolower(substr(strrchr($file, "."), 1));
}
/**
* 遍历目录内容
* @param string $dirName 目录名
* @param string $exts 读取的文件扩展名
* @param int $son 是否显示子目录
* @param array $list
* @return array
*/
static public function tree($dirName = null, $exts = '', $son = 0, $list = array())
{
if (is_null($dirName)) $dirName = '.';
$dirPath = self::dirPath($dirName);
static $id = 0;
if (is_array($exts))
$exts = implode("|", $exts);
foreach (glob($dirPath . '*') as $v) {
$id++;
if (is_dir($v) || !$exts || preg_match("/\.($exts)/i", $v)) {
$list [$id] ['type'] = filetype($v);
$list [$id] ['filename'] = basename($v);
$path = str_replace("\\", "/", realpath($v)) . (is_dir($v) ? '/' : '');
$list [$id] ['path']=$path;
$list [$id] ['spath']=ltrim(str_replace(dirname($_SERVER['SCRIPT_FILENAME']),'',$path),'/');
$list [$id] ['filemtime'] = filemtime($v);
$list [$id] ['fileatime'] = fileatime($v);
$list [$id] ['size'] = is_file($v) ? filesize($v) : self::get_dir_size($v);
$list [$id] ['iswrite'] = is_writeable($v) ? 1 : 0;
$list [$id] ['isread'] = is_readable($v) ? 1 : 0;
}
if ($son) {
if (is_dir($v)) {
$list = self::tree($v, $exts, $son = 1, $list);
}
}
}
return $list;
}
static public function get_dir_size($f)
{
$s = 0;
foreach (glob($f . '/*') as $v) {
$s += is_file($v) ? filesize($v) : self::get_dir_size($v);
}
return $s;
}
/**
* 只显示目录树
* @param null $dirName 目录名
* @param int $son
* @param int $pid 父目录ID
* @param array $dirs 目录列表
* @return array
*/
static public function treeDir($dirName = null, $son = 0, $pid = 0, $dirs = array())
{
if (!$dirName) $dirName = '.';
static $id = 0;
$dirPath = self::dirPath($dirName);
foreach (glob($dirPath . "*") as $v) {
if (is_dir($v)) {
$id++;
$dirs [$id] = array("id" => $id, 'pid' => $pid, "dirname" => basename($v), "dirpath" => $v);
if ($son) {
$dirs = self::treeDir($v, $son, $id, $dirs);
}
}
}
return $dirs;
}
/**
* 删除目录及文件,支持多层删除目录
* @param string $dirName 目录名
* @return bool
*/
static public function del($dirName)
{
if (is_file($dirName)) {
unlink($dirName);
return true;
}
$dirPath = self::dirPath($dirName);
if (!is_dir($dirPath)) return true;
foreach (glob($dirPath . "*") as $v) {
is_dir($v) ? self::del($v) : unlink($v);
}
return @rmdir($dirName);
}
/**
* 批量创建目录
* @param $dirName 目录名数组
* @param int $auth 权限
* @return bool
*/
static public function create($dirName, $auth = 0755)
{
$dirPath = self::dirPath($dirName);
if (is_dir($dirPath))
return true;
$dirs = explode('/', $dirPath);
$dir = '';
foreach ($dirs as $v) {
$dir .= $v . '/';
if (is_dir($dir))
continue;
mkdir($dir, $auth);
}
return is_dir($dirPath);
}
/**
* 复制目录
* @param string $olddir 原目录
* @param string $newdir 目标目录
* @param bool $strip_space 去空白去注释
* @return bool
*/
static public function copy($olddir, $newdir, $strip_space = false)
{
$olddir = self::dirPath($olddir);
$newdir = self::dirPath($newdir);
if (!is_dir($olddir))
error("复制失败:" . $olddir . "目录不存在");
if (!is_dir($newdir))
self::create($newdir);
foreach (glob($olddir . '*') as $v) {
$to = $newdir . basename($v);
if (is_file($to))
continue;
if (is_dir($v)) {
self::copy($v, $to, $strip_space);
} else {
if ($strip_space) {
$data = file_get_contents($v);
file_put_contents($to, strip_space($data));
} else {
copy($v, $to);
}
chmod($to, "0777");
}
}
return true;
}
/**
* 目录下创建安全文件
* @param $dirName 操作目录
* @param bool $recursive 为true会递归的对子目录也创建安全文件
*/
static public function safeFile($dirName, $recursive = false)
{
$file = LIB_PATH . '/index.html';
if (!is_dir($dirName)) {
return;
}
$dirPath = self::dirPath($dirName);
/**
* 创建安全文件
*/
if (!is_file($dirPath . 'index.html')) {
copy($file, $dirPath . 'index.html');
}
/**
* 操作子目录
*/
if ($recursive) {
foreach (glob($dirPath . "*") as $d) {
is_dir($d) and self::safeFile($d);
}
}
}
}