作者 lyh

gxdemo脚本

@@ -66,28 +66,31 @@ class FileManageController extends BaseController @@ -66,28 +66,31 @@ class FileManageController extends BaseController
66 { 66 {
67 // 检查参数 67 // 检查参数
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('参数错误:path 参数缺失或为空', Code::SYSTEM_ERROR);
70 } 70 }
71 - // 获取文件名和 URL 71 +
  72 + // 获取文件名和完整 URL
72 $username = basename($this->param['path']); 73 $username = basename($this->param['path']);
73 $parsed_url = parse_url($this->param['path']); 74 $parsed_url = parse_url($this->param['path']);
74 $fileUrl = isset($parsed_url['scheme']) 75 $fileUrl = isset($parsed_url['scheme'])
75 ? $this->param['path'] 76 ? $this->param['path']
76 - : 'https://file.globalso.com' . $this->param['path']; 77 + : 'https://v6-file.globalso.com' . $this->param['path'];
  78 +
77 // 获取文件头信息 79 // 获取文件头信息
78 - $headers = get_headers($fileUrl, 1);  
79 - if ($headers === false || !isset($headers['Content-Length'])) {  
80 - $this->response('无法获取文件信息', Code::SYSTEM_ERROR); 80 + $headers = @get_headers($fileUrl, 1);
  81 + if (!$headers || empty($headers['Content-Length'])) {
  82 + $this->response('无法获取文件信息或文件不存在', Code::SYSTEM_ERROR);
81 } 83 }
82 $fileSize = $headers['Content-Length'] ?? 0; 84 $fileSize = $headers['Content-Length'] ?? 0;
83 $contentType = $headers['Content-Type'] ?? 'application/octet-stream'; 85 $contentType = $headers['Content-Type'] ?? 'application/octet-stream';
  86 + if ($fileSize <= 0) {
  87 + $this->response('文件大小无效', Code::SYSTEM_ERROR);
  88 + }
84 // 设置响应头 89 // 设置响应头
85 header('Content-Description: File Transfer'); 90 header('Content-Description: File Transfer');
86 header('Content-Type: ' . $contentType); 91 header('Content-Type: ' . $contentType);
87 header('Content-Disposition: attachment; filename="' . $username . '"'); 92 header('Content-Disposition: attachment; filename="' . $username . '"');
88 - if ($fileSize > 0) {  
89 - header('Content-Length: ' . $fileSize);  
90 - } 93 + header('Content-Length: ' . $fileSize);
91 header('Cache-Control: must-revalidate'); 94 header('Cache-Control: must-revalidate');
92 header('Pragma: public'); 95 header('Pragma: public');
93 // 初始化 cURL 96 // 初始化 cURL
@@ -104,11 +107,12 @@ class FileManageController extends BaseController @@ -104,11 +107,12 @@ class FileManageController extends BaseController
104 // 分块输出文件内容 107 // 分块输出文件内容
105 curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) { 108 curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
106 echo $data; 109 echo $data;
107 - flush(); // 强制输出 110 + flush(); // 强制输出缓冲
108 return strlen($data); 111 return strlen($data);
109 }); 112 });
  113 + // 清空缓冲区
  114 + @ob_end_clean();
110 // 执行 cURL 115 // 执行 cURL
111 - ob_end_clean(); // 清空输出缓冲  
112 curl_exec($ch); 116 curl_exec($ch);
113 // 检查 HTTP 状态码和 cURL 错误 117 // 检查 HTTP 状态码和 cURL 错误
114 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 118 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@@ -119,6 +123,7 @@ class FileManageController extends BaseController @@ -119,6 +123,7 @@ class FileManageController extends BaseController
119 curl_close($ch); 123 curl_close($ch);
120 $this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR); 124 $this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR);
121 } 125 }
  126 + // 关闭 cURL
122 curl_close($ch); 127 curl_close($ch);
123 exit; 128 exit;
124 } 129 }
@@ -126,6 +131,7 @@ class FileManageController extends BaseController @@ -126,6 +131,7 @@ class FileManageController extends BaseController
126 131
127 132
128 133
  134 +
129 public function upload(Request $request, FileManage $fileManage){ 135 public function upload(Request $request, FileManage $fileManage){
130 $request->validate([ 136 $request->validate([
131 'file'=>['required'], 137 'file'=>['required'],