PHP-header()生成文件

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
<?php

public function httpHeader(){
$id = 21;

$sql = "select pass from user where id=" . $id;

$pass = $mysql->getResult($sql);


//需要经获取到的pass以文件形式,让用户可以点击下载到本地

header("Content-type:text/plain");

header("Accept-Ranges:bytes");

header("Content-Disposition:attachement; filename=" . $pas . ".txt");

header("Cache-Control:must-revalidate,post-check=0,pre-check=0");

header("Pragma:no-cache");


echo $pass;

}

将生成的文件放到一个a链接中:

1
<a href="/index/httpHeader">点击下载文件</a>

这样就可以简单的通过在线生成文件,不需要再存储到后台服务器。