作者 lyh

gxdemo脚本

... ... @@ -10,6 +10,7 @@ use App\Models\FileManage\FileManage;
use App\Models\Project\Project;
use App\Services\CosService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
/**
* 文件管理
... ... @@ -75,52 +76,15 @@ class FileManageController extends BaseController
? $this->param['path']
: 'https://file.globalso.com' . $this->param['path'];
// 获取文件头信息
$headers = get_headers($fileUrl, 1);
if ($headers === false || !isset($headers['Content-Length'])) {
$this->response('无法获取文件信息', Code::SYSTEM_ERROR);
// Download the file
$response = Http::get($fileUrl);
if ($response->successful()) {
// Save the file locally
Storage::put($username, $response->body());
$this->response('success');
} else {
$this->response('error');
}
$fileSize = $headers['Content-Length'] ?? 0;
$contentType = $headers['Content-Type'] ?? 'application/octet-stream';
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: ' . $contentType);
header('Content-Disposition: attachment; filename="' . $username . '"');
if ($fileSize > 0) {
header('Content-Length: ' . $fileSize);
}
header('Cache-Control: must-revalidate');
header('Pragma: public');
// 初始化 cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8192); // 设置缓冲区大小
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 分块输出文件内容
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
echo $data;
flush(); // 强制输出
return strlen($data);
});
// 执行 cURL
ob_end_clean(); // 清空输出缓冲
curl_exec($ch);
// 检查 HTTP 状态码和 cURL 错误
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch) || $httpCode != 200) {
$errorMsg = curl_errno($ch)
? 'cURL 错误: ' . curl_error($ch)
: 'HTTP 错误: ' . $httpCode;
curl_close($ch);
$this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR);
}
curl_close($ch);
exit;
}
... ...