PHP文件下载代码

其一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

$filename = $_GET['filename'];

// 修改这一行设置你的文件下载目录
$download_path = "ficheros/";

// 不能下载上一层目录的文件
if(eregi("\.\.", $filename)) die("抱歉,你不能下载该文件!");
$file = str_replace("..", "", $filename);

// 包含 .ht 的文件不能下载
if(eregi("\.ht.+", $filename)) die("抱歉,你不能下载该文件!");

// 创建文件下载路径
$file = "$download_path$file";

// 判断文件是否存在
if(!file_exists($file)) die("抱歉,文件不存在!");

// 文件类型,作为头部发送给浏览器
$type = filetype($file);

// 获取时间和日期
$today = date("F j, Y, g:i a");
$time = time();

// 发送文件头部
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// 发送文件内容
set_time_limit(0);
readfile($file);

?>

其二

  • 其实这算html常规方式(
1
2
3
4
<button>
<a href = "http://localhost/down.zip">
下载文件
</button>

其三

传递参数:

1
2
3
4
<button>
<a href = "http://localhost?f='down'">
下载文件
</button>

查找文件并挑战到下载链接:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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');
}
作者

xeonds

发布于

2021-07-18

更新于

2024-05-13

许可协议

评论