作者 lyh

gx下载文件错误问题

@@ -68,20 +68,36 @@ class FileManageController extends BaseController @@ -68,20 +68,36 @@ 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('参数错误:path 参数缺失或为空', Code::SYSTEM_ERROR); 69 $this->response('参数错误:path 参数缺失或为空', Code::SYSTEM_ERROR);
70 } 70 }
71 -  
72 // 获取文件名和完整 URL 71 // 获取文件名和完整 URL
73 $username = basename($this->param['path']); 72 $username = basename($this->param['path']);
74 $parsed_url = parse_url($this->param['path']); 73 $parsed_url = parse_url($this->param['path']);
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'];
78 - // 获取文件头信息  
79 - $headers = @get_headers($fileUrl, 1);  
80 - if (!$headers || !isset($headers['Content-Length']) || $headers['Content-Length'] <= 0) { 77 + // 使用 cURL 获取文件头信息
  78 + $ch = curl_init();
  79 + curl_setopt($ch, CURLOPT_URL, $fileUrl);
  80 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 不直接输出,而是返回结果
  81 + curl_setopt($ch, CURLOPT_HEADER, true); // 获取响应头
  82 + curl_setopt($ch, CURLOPT_NOBODY, true); // 只请求头部,不下载实际文件
  83 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向
  84 + curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时时间
  85 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证
  86 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  87 + $response = curl_exec($ch);
  88 + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  89 + $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  90 + curl_close($ch);
  91 + // 检查文件大小和状态码
  92 + if ($httpCode != 200 || $fileSize <= 0) {
81 $this->response('无法获取文件信息或文件不存在', Code::SYSTEM_ERROR); 93 $this->response('无法获取文件信息或文件不存在', Code::SYSTEM_ERROR);
82 } 94 }
83 - $fileSize = $headers['Content-Length'];  
84 - $contentType = $headers['Content-Type'] ?? 'application/octet-stream'; 95 +
  96 + // 获取文件类型
  97 + $contentType = 'application/octet-stream'; // 默认类型为二进制流
  98 + if (preg_match('/Content-Type: (.+)/i', $response, $matches)) {
  99 + $contentType = $matches[1];
  100 + }
85 // 设置响应头 101 // 设置响应头
86 header('Content-Description: File Transfer'); 102 header('Content-Description: File Transfer');
87 header('Content-Type: ' . $contentType); 103 header('Content-Type: ' . $contentType);
@@ -90,11 +106,11 @@ class FileManageController extends BaseController @@ -90,11 +106,11 @@ class FileManageController extends BaseController
90 header('Cache-Control: must-revalidate'); 106 header('Cache-Control: must-revalidate');
91 header('Pragma: public'); 107 header('Pragma: public');
92 header('Expires: 0'); 108 header('Expires: 0');
93 - // 清空所有输出缓冲区 109 + // 清空输出缓冲区
94 while (ob_get_level() > 0) { 110 while (ob_get_level() > 0) {
95 ob_end_clean(); 111 ob_end_clean();
96 } 112 }
97 - // 初始化 cURL 113 + // 初始化 cURL 下载文件
98 $ch = curl_init(); 114 $ch = curl_init();
99 curl_setopt($ch, CURLOPT_URL, $fileUrl); 115 curl_setopt($ch, CURLOPT_URL, $fileUrl);
100 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 直接输出内容 116 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 直接输出内容