作者 lyh

gxdemo脚本

@@ -10,7 +10,6 @@ use App\Models\FileManage\FileManage; @@ -10,7 +10,6 @@ use App\Models\FileManage\FileManage;
10 use App\Models\Project\Project; 10 use App\Models\Project\Project;
11 use App\Services\CosService; 11 use App\Services\CosService;
12 use Illuminate\Http\Request; 12 use Illuminate\Http\Request;
13 -use Illuminate\Support\Facades\Http;  
14 13
15 /** 14 /**
16 * 文件管理 15 * 文件管理
@@ -76,13 +75,52 @@ class FileManageController extends BaseController @@ -76,13 +75,52 @@ class FileManageController extends BaseController
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 // 获取文件头信息 77 // 获取文件头信息
79 - // Download the file  
80 - $response = Http::get($fileUrl);  
81 - if ($response->successful()) {  
82 - $this->response('success');  
83 - } else {  
84 - $this->response('error');  
85 - } 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, 8192); // 设置缓冲区大小
  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;
  119 + curl_close($ch);
  120 + $this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR);
  121 + }
  122 + curl_close($ch);
  123 + exit;
86 } 124 }
87 125
88 126