正在显示
1 个修改的文件
包含
58 行增加
和
52 行删除
| @@ -64,63 +64,69 @@ class FileManageController extends BaseController | @@ -64,63 +64,69 @@ class FileManageController extends BaseController | ||
| 64 | */ | 64 | */ |
| 65 | public function downLoad() | 65 | public function downLoad() |
| 66 | { | 66 | { |
| 67 | - // 检查参数 | ||
| 68 | - if (!isset($this->param['path']) || empty($this->param['path'])) { | ||
| 69 | - $this->response('参数错误', Code::SYSTEM_ERROR); | 67 | + if(!isset($this->param['path']) || empty($this->param['path'])){ |
| 68 | + $this->response('参数错误',Code::SYSTEM_ERROR); | ||
| 70 | } | 69 | } |
| 71 | - // 获取文件名和 URL | ||
| 72 | $username = basename($this->param['path']); | 70 | $username = basename($this->param['path']); |
| 73 | $parsed_url = parse_url($this->param['path']); | 71 | $parsed_url = parse_url($this->param['path']); |
| 74 | - $fileUrl = isset($parsed_url['scheme']) | ||
| 75 | - ? $this->param['path'] | ||
| 76 | - : 'https://file.globalso.com' . $this->param['path']; | ||
| 77 | - // 获取文件头信息 | ||
| 78 | - $headers = get_headers($fileUrl, 1); | ||
| 79 | - if ($headers === false || !isset($headers['Content-Length'])) { | ||
| 80 | - $this->response('无法获取文件信息', Code::SYSTEM_ERROR); | ||
| 81 | - } | ||
| 82 | - $fileSize = $headers['Content-Length'] ?? 0; | ||
| 83 | - $contentType = $headers['Content-Type'] ?? 'application/octet-stream'; | ||
| 84 | - // 设置响应头 | ||
| 85 | - header('Content-Description: File Transfer'); | ||
| 86 | - header('Content-Type: ' . $contentType); | ||
| 87 | - header('Content-Disposition: attachment; filename="' . $username . '"'); | ||
| 88 | - if ($fileSize > 0) { | ||
| 89 | - header('Content-Length: ' . $fileSize); | ||
| 90 | - } | ||
| 91 | - header('Cache-Control: must-revalidate'); | ||
| 92 | - header('Pragma: public'); | ||
| 93 | - // 初始化 cURL | ||
| 94 | - $ch = curl_init(); | ||
| 95 | - curl_setopt($ch, CURLOPT_URL, $fileUrl); | ||
| 96 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); | ||
| 97 | - curl_setopt($ch, CURLOPT_HEADER, false); | ||
| 98 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
| 99 | - curl_setopt($ch, CURLOPT_BUFFERSIZE, 18192); // 设置缓冲区大小 | ||
| 100 | - curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间 | ||
| 101 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | ||
| 102 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
| 103 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | ||
| 104 | - // 分块输出文件内容 | ||
| 105 | - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) { | ||
| 106 | - echo $data; | ||
| 107 | - flush(); // 强制输出 | ||
| 108 | - return strlen($data); | ||
| 109 | - }); | ||
| 110 | - // 执行 cURL | ||
| 111 | - ob_end_clean(); // 清空输出缓冲 | ||
| 112 | - curl_exec($ch); | ||
| 113 | - // 检查 HTTP 状态码和 cURL 错误 | ||
| 114 | - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 115 | - if (curl_errno($ch) || $httpCode != 200) { | ||
| 116 | - $errorMsg = curl_errno($ch) | ||
| 117 | - ? 'cURL 错误: ' . curl_error($ch) | ||
| 118 | - : 'HTTP 错误: ' . $httpCode; | 72 | + if(isset($parsed_url['scheme'])){ |
| 73 | + $fileUrl = $this->param['path']; | ||
| 74 | + // 获取文件头信息 | ||
| 75 | + $headers = get_headers($fileUrl, 1); | ||
| 76 | + if ($headers === false || !isset($headers['Content-Length'])) { | ||
| 77 | + $this->response('无法获取文件信息', Code::SYSTEM_ERROR); | ||
| 78 | + } | ||
| 79 | + $fileSize = $headers['Content-Length'] ?? 0; | ||
| 80 | + $contentType = $headers['Content-Type'] ?? 'application/octet-stream'; | ||
| 81 | + // 设置响应头 | ||
| 82 | + header('Content-Description: File Transfer'); | ||
| 83 | + header('Content-Type: ' . $contentType); | ||
| 84 | + header('Content-Disposition: attachment; filename="' . $username . '"'); | ||
| 85 | + if ($fileSize > 0) { | ||
| 86 | + header('Content-Length: ' . $fileSize); | ||
| 87 | + } | ||
| 88 | + header('Cache-Control: must-revalidate'); | ||
| 89 | + header('Pragma: public'); | ||
| 90 | + // 初始化 cURL | ||
| 91 | + $ch = curl_init(); | ||
| 92 | + curl_setopt($ch, CURLOPT_URL, $fileUrl); | ||
| 93 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); | ||
| 94 | + curl_setopt($ch, CURLOPT_HEADER, false); | ||
| 95 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
| 96 | + curl_setopt($ch, CURLOPT_BUFFERSIZE, 18192); // 设置缓冲区大小 | ||
| 97 | + curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间 | ||
| 98 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | ||
| 99 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
| 100 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | ||
| 101 | + // 分块输出文件内容 | ||
| 102 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) { | ||
| 103 | + echo $data; | ||
| 104 | + flush(); // 强制输出 | ||
| 105 | + return strlen($data); | ||
| 106 | + }); | ||
| 107 | + // 执行 cURL | ||
| 108 | + ob_end_clean(); // 清空输出缓冲 | ||
| 109 | + curl_exec($ch); | ||
| 110 | + // 检查 HTTP 状态码和 cURL 错误 | ||
| 111 | + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 112 | + if (curl_errno($ch) || $httpCode != 200) { | ||
| 113 | + $errorMsg = curl_errno($ch) | ||
| 114 | + ? 'cURL 错误: ' . curl_error($ch) | ||
| 115 | + : 'HTTP 错误: ' . $httpCode; | ||
| 116 | + curl_close($ch); | ||
| 117 | + $this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR); | ||
| 118 | + } | ||
| 119 | curl_close($ch); | 119 | curl_close($ch); |
| 120 | - $this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR); | 120 | + exit; |
| 121 | + } else { | ||
| 122 | + $fileUrl = 'https://file.globalso.com'.$this->param['path']; | ||
| 123 | + // 设置响应头 | ||
| 124 | + header('Content-Description: File Transfer'); | ||
| 125 | + header('Content-Type: application/octet-stream'); | ||
| 126 | + header('Content-Disposition: attachment; filename="' . $username . '"'); | ||
| 127 | + // 下载文件 | ||
| 128 | + readfile($fileUrl); | ||
| 121 | } | 129 | } |
| 122 | - curl_close($ch); | ||
| 123 | - exit; | ||
| 124 | } | 130 | } |
| 125 | 131 | ||
| 126 | 132 |
-
请 注册 或 登录 后发表评论