作者 lyh

gx下载文件错误问题

... ... @@ -74,17 +74,14 @@ class FileManageController extends BaseController
$parsed_url = parse_url($this->param['path']);
$fileUrl = isset($parsed_url['scheme'])
? $this->param['path']
: 'https://v6-file.globalso.com' . $this->param['path'];
// 获取文件头信息,验证文件可访问性
: 'https://file.globalso.com' . $this->param['path'];
// 获取文件头信息
$headers = @get_headers($fileUrl, 1);
if (!$headers || empty($headers['Content-Length'])) {
@file_put_contents(storage_path('logs/lyh_error.log'), var_export('1-报错', true) . PHP_EOL, FILE_APPEND);
if (!$headers || !isset($headers['Content-Length']) || $headers['Content-Length'] <= 0) {
$this->response('无法获取文件信息或文件不存在', Code::SYSTEM_ERROR);
}
$fileSize = $headers['Content-Length'] ?? 0;
$fileSize = $headers['Content-Length'];
$contentType = $headers['Content-Type'] ?? 'application/octet-stream';
if ($fileSize <= 0) {
@file_put_contents(storage_path('logs/lyh_error.log'), var_export('2-报错', true) . PHP_EOL, FILE_APPEND);
}
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: ' . $contentType);
... ... @@ -93,15 +90,20 @@ class FileManageController extends BaseController
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Expires: 0');
// 清空所有输出缓冲区
while (ob_get_level() > 0) {
ob_end_clean();
}
// 初始化 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_FOLLOWLOCATION, true); // 跟随重定向
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8192); // 设置缓冲区大小
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 连接超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 分块输出文件内容
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
... ... @@ -109,8 +111,6 @@ class FileManageController extends BaseController
flush(); // 强制输出缓冲
return strlen($data);
});
// 清空输出缓冲
@ob_end_clean();
// 执行 cURL
curl_exec($ch);
// 检查 cURL 错误或 HTTP 状态码
... ... @@ -127,11 +127,6 @@ class FileManageController extends BaseController
exit;
}
public function upload(Request $request, FileManage $fileManage){
$request->validate([
'file'=>['required'],
... ...