3 个回答
download.php?file=work.zip
<?php
$filepath = '/data/'.trim($_GET['file']);
if(file_exists($filepath)) {
    log($filepath);
}
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
//方法1:交给Nginx输出(Nginx有AIO线程池,不会阻塞)
header("X-Accel-Redirect: $filepath");
//方法2:PHP自己输出(PHP进程会被阻塞)
//readfile($filepath);

最简单的方法:用户下载文件先请求,php编写的记录下载的地址,记录下你需要的信息。然后在使用php的 header函数跳转到真正的下载文件地址

<?php
$file_name = '9567b94e440700226e003fb9258dd733.png';  //下载的文件名
header("Content-Disposition:attachment;filename=".$file_name."");
readfile($file_name);
$header = $_SERVER['HTTP_USER_AGENT'];  //用户UA
$ip = $_SERVER['REMOTE_ADDR'];   //用户IP
推荐问题