作者 lyh

gxdemo脚本

... ... @@ -64,63 +64,69 @@ class FileManageController extends BaseController
*/
public function downLoad()
{
// 检查参数
if (!isset($this->param['path']) || empty($this->param['path'])) {
$this->response('参数错误', Code::SYSTEM_ERROR);
if(!isset($this->param['path']) || empty($this->param['path'])){
$this->response('参数错误',Code::SYSTEM_ERROR);
}
// 获取文件名和 URL
$username = basename($this->param['path']);
$parsed_url = parse_url($this->param['path']);
$fileUrl = isset($parsed_url['scheme'])
? $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);
}
$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, 18192); // 设置缓冲区大小
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;
if(isset($parsed_url['scheme'])){
$fileUrl = $this->param['path'];
// 获取文件头信息
$headers = get_headers($fileUrl, 1);
if ($headers === false || !isset($headers['Content-Length'])) {
$this->response('无法获取文件信息', Code::SYSTEM_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, 18192); // 设置缓冲区大小
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);
$this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR);
exit;
} else {
$fileUrl = 'https://file.globalso.com'.$this->param['path'];
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $username . '"');
// 下载文件
readfile($fileUrl);
}
curl_close($ch);
exit;
}
... ...