详情页

php 两种文件下载的实现

时间:2023年10月05日

编辑:佚名

1、直接添加文件链接
<button> <a href = "http://localhost/down.zip"> 下载文件 </button>
2、传递参数查找并跳转到下载链接
传递参数:
<button>
    <a href = "http://localhost?f='down'">
    下载文件
</button>
查找文件并挑战到下载链接:
<?php
$down = $_GET['f'];   //获取文件参数
$filename = $down.'.zip'; //获取文件名称
$dir ="down/";  //相对于网站根目录的下载目录路径
$down_host = $_SERVER['HTTP_HOST'].'/'; //当前域名
//判断如果文件存在,则跳转到下载路径
if(file_exists(__DIR__.'/'.$dir.$filename)){
    header('location:http://'.$down_host.$dir.$filename);
}else{
    header('HTTP/1.1 404 Not Found');
}
相关文章
猜你需要