yunzer/app/index/controller/StorageController.php
2025-06-07 09:15:14 +08:00

70 lines
2.0 KiB
PHP

<?php
/**
* 商业使用授权协议
*
* Copyright (c) 2025 [云泽网]. 保留所有权利.
*
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
*
* 授权购买请联系: 357099073@qq.com
* 官方网站: https://www.yunzer.cn
*
* 评估用户须知:
* 1. 禁止移除版权声明
* 2. 禁止用于生产环境
* 3. 禁止转售或分发
*/
namespace app\index\controller;
use think\facade\Request;
use think\facade\Filesystem;
use think\Response;
use app\index\model\Resources\Resources;
class StorageController extends BaseController
{
/**
* 处理文件下载
* @return \think\Response
*/
public function download()
{
// 获取请求的文件路径
$path = Request::pathinfo();
// 移除 'storage/' 前缀
$path = str_replace('storage/', '', $path);
// 构建完整的文件路径
$filePath = public_path() . 'storage/' . $path;
// 检查文件是否存在
if (!file_exists($filePath)) {
return Response::create('文件不存在', 'html', 404);
}
// 获取文件信息
$fileInfo = pathinfo($filePath);
$fileName = $fileInfo['basename'];
$fileSize = filesize($filePath);
$fileType = mime_content_type($filePath);
// 设置响应头
$headers = [
'Content-Type' => $fileType,
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
'Content-Length' => $fileSize,
'Cache-Control' => 'no-cache, must-revalidate',
'Pragma' => 'no-cache',
'Expires' => '0'
];
// 读取文件内容
$content = file_get_contents($filePath);
// 返回文件下载响应
return Response::create($content, 'file', 200, $headers);
}
}