作者 lyh

gx

... ... @@ -63,22 +63,43 @@ class FileManageController extends BaseController
* @time :2023/12/28 17:18
*/
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);
}
$username = basename($this->param['path']);
$parsed_url = parse_url($this->param['path']);
if(isset($parsed_url['scheme'])){
// 构造文件 URL
if (isset($parsed_url['scheme'])) {
$fileUrl = $this->param['path'];
} else {
$fileUrl = 'https://file.globalso.com'.$this->param['path'];
$fileUrl = 'https://file.globalso.com' . $this->param['path'];
}
// 初始化 curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
// 执行 curl 请求
$fileContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 检查 HTTP 响应码
if ($httpCode != 200 || $fileContent === false) {
$this->response('文件下载失败', Code::SYSTEM_ERROR);
}
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $username . '"');
// 下载文件
readfile($fileUrl);
header('Content-Length: ' . strlen($fileContent));
// 输出文件内容
echo $fileContent;
exit;
}
public function upload(Request $request, FileManage $fileManage){
... ...