img

ThinkPHP5 清除缓存

2020-04-14 0条评论 2k次阅读 PHP


/**
 * @param null $directory
 * @return bool
 * @name: clear_cache
 * @describe:清除系统缓存
 */
function clear_cache($directory = null)
{
    $directory = empty($directory) ? ROOT_PATH . 'runtime/cache/' : $directory;
    if (is_dir($directory) == false) {
        return false;
    }
    $handle = opendir($directory);
    while (($file = readdir($handle)) !== false) {
        if ($file != "." && $file != "..") {
            is_dir($directory . '/' . $file) ?
                clear_cache($directory . '/' . $file) :
                unlink($directory . '/' . $file);
        }
    }
    if (readdir($handle) == false) {
        closedir($handle);
        rmdir($directory);
    }
}


/**
 * @param null $directory
 * @return bool
 * @name: clear_log
 * @describe:清除系统日志
 */
function clear_log($directory = null)
{
    $directory = empty($directory) ? ROOT_PATH . 'runtime/log/' : $directory;
    if (is_dir($directory) == false) {
        return false;
    }
    $handle = opendir($directory);
    while (($file = readdir($handle)) !== false) {
        if ($file != "." && $file != "..") {
            is_dir($directory . '/' . $file) ?
                clear_cache($directory . '/' . $file) :
                unlink($directory . '/' . $file);
        }
    }
    if (readdir($handle) == false) {
        closedir($handle);
        rmdir($directory);
    }
}


/**
 * @param null $directory
 * @return bool
 * @name: clear_temp
 * @describe:刷新系统静态文件
 */
function clear_temp($directory = null)
{
    $directory = empty($directory) ? ROOT_PATH . 'runtime/temp/' : $directory;
    if (is_dir($directory) == false) {
        return false;
    }
    $handle = opendir($directory);
    while (($file = readdir($handle)) !== false) {
        if ($file != "." && $file != "..") {
            is_dir($directory . '/' . $file) ?
                clear_cache($directory . '/' . $file) :
                unlink($directory . '/' . $file);
        }
    }
    if (readdir($handle) == false) {
        closedir($handle);
        rmdir($directory);
    }
}

💬 COMMENT


🦄 支持markdown语法

👋友