马上注册,免费下载更多dz插件网资源。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
分享一个帝国CMS管理七牛附件的功能,页面渲染图如下:
1,首先去七牛去下载他们的php的SDK包。(不知道是不是我的宝塔问题,Composer我选择阿里云会出错,建议腾讯云)
2,因为远程读取要进行分页,占用资源超大(必须安装个Redis。让非第一次加载没有那么卡顿)
废话说完了,开始上代码。不知道能不能发成功。毕竟4414的防火墙要扯淡的
<?php
define('EmpireCMSAdmin','1');
require("../../../class/connect.php");
require("../../../class/db_sql.php");
require("../../../class/functions.php");
require("../../../member/class/user.php");
require '../../'.LoadLang('pub/fun.php');
require ECMS_PATH.'e/extend/Qiniu/autoload.php';//七牛
require ECMS_PATH.'e/extend/Qiniu/qiniu_conf.php';//七牛
$link=db_connect();
$empire=new mysqlquery();
$editor=2;
//验证用户
$lur=is_login();
$logininid=$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=$lur['groupid'];
$loginadminstyleid=$lur['adminstyleid'];
//ehash
$ecms_hashur=hReturnEcmsHashStrAll();
//验证权限
$classid=isset($classid)?$classid:null;
CheckLevel($logininid,$loginin,$classid,"template");
$enews=$_POST['enews'];
// 文件大小转换函数
function formatFileSize($bytes) {
if ($bytes < 1024) {
return $bytes. ' B';
} elseif ($bytes < 1024 * 1024) {
return round($bytes / 1024, 2). ' K';
} elseif ($bytes < 1024 * 1024 * 1024) {
return round($bytes / (1024 * 1024), 2). ' M';
} else {
return round($bytes / (1024 * 1024 * 1024), 2). ' G';
}
}
$Auth = new \Qiniu\Auth($QiniuConf['ak'],$QiniuConf['sk']);
$bucketMgr = new \Qiniu\Storage\BucketManager($Auth);
// 删除文件的函数
function deleteFile($bucketMgr, $bucket, $key) {
list($ret, $err) = $bucketMgr->delete($bucket, $key);
if ($err!== null) {
return ['success' => false,'message' => "删除文件失败: ". $err->message()];
}
return ['success' => true,'message' => "文件删除成功!"];
}
// 处理删除请求
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['key'])) {
$key = $_GET['key'];
$result = deleteFile($bucketMgr, $QiniuConf['bucket'], $key);
if ($result['success']) {
printerror('删除成功','../../extend/Qiniu/index.php',0,0,1);
exit;
} else {
printerror('删除失败','../../extend/Qiniu/index.php',0,0,1);
}
}
// 获取当前页码
$page = (int)$_GET['page'];
$start = 0;
$line = 20; // 每页显示条数
$page_line = 18; // 每页显示链接数
$offset = $page * $line; // 总偏移量
$prefix = isset($_GET['prefix']) ? $_GET['prefix'] : '';// 根据查询条件和分页参数生成缓存键
$cacheKey = 'qiniu_file_list_' . md5($prefix. '_page_' . $page. '_line_' . $line);
$cachedData = $redis->get($cacheKey);// 尝试从 Redis 中获取缓存数据
if ($cachedData!== false) {
$data = json_decode($cachedData, true);
if ($data && isset($data['currentPageItems'], $data['imageCount'], $data['audioVideoCount'], $data['archiveCount'], $data['num'])) {
$currentPageItems = $data['currentPageItems'];
$imageCount = $data['imageCount'];
$audioVideoCount = $data['audioVideoCount'];
$archiveCount = $data['archiveCount'];
$num = $data['num'];
} else {
$cachedData = false;
}
}
if ($cachedData === false) {
$bucket = $QiniuConf['bucket'];// 存储空间名称
$marker = '';// 列举文件的起始标记,首次列举时为空
$delimiter = '';// 列举文件的分隔符,若为空则不进行分隔
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
$audioVideoExtensions = ['mp3', 'wav', 'avi', 'mp4', 'mov'];
$archiveExtensions = ['zip', 'rar'];
// 初始化计数器
$imageCount = 0;
$audioVideoCount = 0;
$archiveCount = 0;
$totalItems = [];
$limit = 50; // 每次获取 50 条记录
do {
list($ret, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit, $delimiter);
if ($err!== null) {
echo "列举文件失败: ". $err->message();
break;
}
foreach ($ret['items'] as $item) {
// 判断是否为文件夹,这里以文件大小不为 0 且文件名不以 / 结尾作为判断依据
if ($item['fsize'] > 0 && substr($item['key'], -1)!== '/') {
$totalItems[] = $item;
$extension = pathinfo($item['key'], PATHINFO_EXTENSION);// 获取文件扩展名
$extension = strtolower($extension);
// 根据扩展名进行分类统计
if (in_array($extension, $imageExtensions)) {
$imageCount++;
} elseif (in_array($extension, $audioVideoExtensions)) {
$audioVideoCount++;
} elseif (in_array($extension, $archiveExtensions)) {
$archiveCount++;
}
}
}
$marker = isset($ret['marker']) ? $ret['marker'] : '';
} while ($marker!== '');
$num = count($totalItems);
$currentPageItems = array_slice($totalItems, $offset, $line); // 截取当前页需要显示的文件
$data = [
'currentPageItems' => $currentPageItems,
'imageCount' => $imageCount,
'audioVideoCount' => $audioVideoCount,
'archiveCount' => $archiveCount,
'num' => $num
];
$redis->setex($cacheKey, 3600, json_encode($data));
}
$search = '';
$returnpage = page2($num, $line, $page_line, $start, $page, $search);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>七牛云配置管理</title>
<link href="../../adminstyle/<?=$loginadminstyleid?>/adminstyle.css" rel="stylesheet" type="text/css">
<style type="text/css">
form{margin:0;padding:0}
©DZ插件网所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。 本站内容为站长个人技术研究记录或网络,不提供用户交互功能,所有内容版权归原作者所有。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,得到更好的正版服务。 您在本站任何的赞助购买、下载、查阅、回复等行为等均表示接受并同意签订《DZ插件网免责声明协议》。 如有侵权请邮件与我们联系处理: discuzaddons@vip.qq.com 并出示相关证明以便删除。敬请谅解!
|