通过php鉴权,文件下载由 nginx 实现;
比使用php readfile() 高效;# nginx 配置location /auth_download {
internal; # 这个设置是必需的
alias /data/upload; # 文件所在真实路径}<?phpif (!isset($_GET['file'])) { die('文件不存在');
}
$file = $_GET['file'];
$rename = isset($_GET['rename']) ? $_GET['rename'] : $file;// 模拟校验下载权限$canDownload = rand(1, 2) == 1;if ($canDownload) {
header('Content-Type:application/octet-stream;');
header('Content-Disposition: attachment; filename=' . $rename);
header('X-Accel-Redirect: /auth_download/' . ltrim($file, '/'));
} else { echo '无权限';
}
比如下载的文件为: /data/upload/1.zip,php 代码只要
header('X-Accel-Redirect: /auth_download/1.zip') 即可下载对应文件;
作者:forever_youyou
链接:https://www.jianshu.com/p/e6b6c7be15de