作者 lyh

gx

... ... @@ -267,4 +267,30 @@ class FileController
}
return $data;
}
/**
* @remark :文件下载
* @name :downLoad
* @author :lyh
* @method :post
* @time :2023/6/26 16:28
*/
public function downLoad($hash){
$file_model = new File();
$info = $file_model->read(['hash' => $hash]);
if ($info === false) {
$this->response('指定文件不存在!', Code::USER_ERROR);
}
$path = $info['path'];
if (!is_file($path)) {
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
}
$fileUrl = url('public/upload/files/'.basename($path)); // 文件的 URL
$fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名
// 设置响应头
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
// 下载文件
readfile($fileUrl);
}
}
... ...