php实现文件下载

php实现文件下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Header ( "Content-type: application/octet-stream" );
$ua = $_SERVER ["HTTP_USER_AGENT"];
$file = '/var/www/tmp.txt';
$filename = basename ( $file );
$encoded_filename = rawurlencode ( $filename );
if (preg_match ( "/MSIE/", $ua )) {
header ( 'Content-Disposition: attachment; filename="' . $encoded_filename . '"' );
} else if (preg_match ( "/Firefox/", $ua )) {
header ( "Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"' );
} else {
header ( 'Content-Disposition: attachment; filename="' . $filename . '"' );
}
header ( "Content-Length: " . filesize ( $file ) );
readfile ( $file );