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