53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
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);
|
|
}
|
|
} |