作者 lyh

gx下载文件错误问题

@@ -74,17 +74,14 @@ class FileManageController extends BaseController @@ -74,17 +74,14 @@ class FileManageController extends BaseController
74 $parsed_url = parse_url($this->param['path']); 74 $parsed_url = parse_url($this->param['path']);
75 $fileUrl = isset($parsed_url['scheme']) 75 $fileUrl = isset($parsed_url['scheme'])
76 ? $this->param['path'] 76 ? $this->param['path']
77 - : 'https://v6-file.globalso.com' . $this->param['path'];  
78 - // 获取文件头信息,验证文件可访问性 77 + : 'https://file.globalso.com' . $this->param['path'];
  78 + // 获取文件头信息
79 $headers = @get_headers($fileUrl, 1); 79 $headers = @get_headers($fileUrl, 1);
80 - if (!$headers || empty($headers['Content-Length'])) {  
81 - @file_put_contents(storage_path('logs/lyh_error.log'), var_export('1-报错', true) . PHP_EOL, FILE_APPEND); 80 + if (!$headers || !isset($headers['Content-Length']) || $headers['Content-Length'] <= 0) {
  81 + $this->response('无法获取文件信息或文件不存在', Code::SYSTEM_ERROR);
82 } 82 }
83 - $fileSize = $headers['Content-Length'] ?? 0; 83 + $fileSize = $headers['Content-Length'];
84 $contentType = $headers['Content-Type'] ?? 'application/octet-stream'; 84 $contentType = $headers['Content-Type'] ?? 'application/octet-stream';
85 - if ($fileSize <= 0) {  
86 - @file_put_contents(storage_path('logs/lyh_error.log'), var_export('2-报错', true) . PHP_EOL, FILE_APPEND);  
87 - }  
88 // 设置响应头 85 // 设置响应头
89 header('Content-Description: File Transfer'); 86 header('Content-Description: File Transfer');
90 header('Content-Type: ' . $contentType); 87 header('Content-Type: ' . $contentType);
@@ -93,15 +90,20 @@ class FileManageController extends BaseController @@ -93,15 +90,20 @@ class FileManageController extends BaseController
93 header('Cache-Control: must-revalidate'); 90 header('Cache-Control: must-revalidate');
94 header('Pragma: public'); 91 header('Pragma: public');
95 header('Expires: 0'); 92 header('Expires: 0');
  93 + // 清空所有输出缓冲区
  94 + while (ob_get_level() > 0) {
  95 + ob_end_clean();
  96 + }
96 // 初始化 cURL 97 // 初始化 cURL
97 $ch = curl_init(); 98 $ch = curl_init();
98 curl_setopt($ch, CURLOPT_URL, $fileUrl); 99 curl_setopt($ch, CURLOPT_URL, $fileUrl);
99 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 直接输出内容 100 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 直接输出内容
100 curl_setopt($ch, CURLOPT_HEADER, false); 101 curl_setopt($ch, CURLOPT_HEADER, false);
101 - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 102 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向
102 curl_setopt($ch, CURLOPT_BUFFERSIZE, 8192); // 设置缓冲区大小 103 curl_setopt($ch, CURLOPT_BUFFERSIZE, 8192); // 设置缓冲区大小
103 curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间 104 curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间
104 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 105 + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 连接超时时间
  106 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证
105 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 107 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
106 // 分块输出文件内容 108 // 分块输出文件内容
107 curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) { 109 curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
@@ -109,8 +111,6 @@ class FileManageController extends BaseController @@ -109,8 +111,6 @@ class FileManageController extends BaseController
109 flush(); // 强制输出缓冲 111 flush(); // 强制输出缓冲
110 return strlen($data); 112 return strlen($data);
111 }); 113 });
112 - // 清空输出缓冲  
113 - @ob_end_clean();  
114 // 执行 cURL 114 // 执行 cURL
115 curl_exec($ch); 115 curl_exec($ch);
116 // 检查 cURL 错误或 HTTP 状态码 116 // 检查 cURL 错误或 HTTP 状态码
@@ -127,11 +127,6 @@ class FileManageController extends BaseController @@ -127,11 +127,6 @@ class FileManageController extends BaseController
127 exit; 127 exit;
128 } 128 }
129 129
130 -  
131 -  
132 -  
133 -  
134 -  
135 public function upload(Request $request, FileManage $fileManage){ 130 public function upload(Request $request, FileManage $fileManage){
136 $request->validate([ 131 $request->validate([
137 'file'=>['required'], 132 'file'=>['required'],