正在显示
1 个修改的文件
包含
27 行增加
和
6 行删除
| @@ -63,22 +63,43 @@ class FileManageController extends BaseController | @@ -63,22 +63,43 @@ class FileManageController extends BaseController | ||
| 63 | * @time :2023/12/28 17:18 | 63 | * @time :2023/12/28 17:18 |
| 64 | */ | 64 | */ |
| 65 | public function downLoad(){ | 65 | public function downLoad(){ |
| 66 | - if(!isset($this->param['path']) || empty($this->param['path'])){ | ||
| 67 | - $this->response('参数错误',Code::SYSTEM_ERROR); | 66 | + // 检查参数 |
| 67 | + if (!isset($this->param['path']) || empty($this->param['path'])) { | ||
| 68 | + $this->response('参数错误', Code::SYSTEM_ERROR); | ||
| 68 | } | 69 | } |
| 70 | + | ||
| 69 | $username = basename($this->param['path']); | 71 | $username = basename($this->param['path']); |
| 70 | $parsed_url = parse_url($this->param['path']); | 72 | $parsed_url = parse_url($this->param['path']); |
| 71 | - if(isset($parsed_url['scheme'])){ | 73 | + |
| 74 | + // 构造文件 URL | ||
| 75 | + if (isset($parsed_url['scheme'])) { | ||
| 72 | $fileUrl = $this->param['path']; | 76 | $fileUrl = $this->param['path']; |
| 73 | } else { | 77 | } else { |
| 74 | - $fileUrl = 'https://file.globalso.com'.$this->param['path']; | 78 | + $fileUrl = 'https://file.globalso.com' . $this->param['path']; |
| 79 | + } | ||
| 80 | + // 初始化 curl | ||
| 81 | + $ch = curl_init(); | ||
| 82 | + curl_setopt($ch, CURLOPT_URL, $fileUrl); | ||
| 83 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| 84 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
| 85 | + curl_setopt($ch, CURLOPT_HEADER, false); | ||
| 86 | + // 执行 curl 请求 | ||
| 87 | + $fileContent = curl_exec($ch); | ||
| 88 | + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 89 | + curl_close($ch); | ||
| 90 | + // 检查 HTTP 响应码 | ||
| 91 | + if ($httpCode != 200 || $fileContent === false) { | ||
| 92 | + $this->response('文件下载失败', Code::SYSTEM_ERROR); | ||
| 75 | } | 93 | } |
| 76 | // 设置响应头 | 94 | // 设置响应头 |
| 77 | header('Content-Description: File Transfer'); | 95 | header('Content-Description: File Transfer'); |
| 78 | header('Content-Type: application/octet-stream'); | 96 | header('Content-Type: application/octet-stream'); |
| 79 | header('Content-Disposition: attachment; filename="' . $username . '"'); | 97 | header('Content-Disposition: attachment; filename="' . $username . '"'); |
| 80 | - // 下载文件 | ||
| 81 | - readfile($fileUrl); | 98 | + header('Content-Length: ' . strlen($fileContent)); |
| 99 | + | ||
| 100 | + // 输出文件内容 | ||
| 101 | + echo $fileContent; | ||
| 102 | + exit; | ||
| 82 | } | 103 | } |
| 83 | 104 | ||
| 84 | public function upload(Request $request, FileManage $fileManage){ | 105 | public function upload(Request $request, FileManage $fileManage){ |
-
请 注册 或 登录 后发表评论