php实现下载

PHP实现下载文件的两种方法。分享下,有用到的朋友看看哦。

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/**
* 下载文件
* header函数
*
*/
header('Content-Description: File Transfer');
 
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0′);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($file_path);
?>

了解php中header函数的用法。

方法二:

1
2
3
4
5
6
7
8
9
10
<?php
//文件下载
//readfile
$fileinfo = pathinfo($filename);
header('Content-type: application/x-'.$fileinfo['extension']);
header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
header('Content-Length: '.filesize($filename));
readfile($thefile);
exit();
?>

转自http://www.jb51.net/article/40485.htm

原文地址:https://www.cnblogs.com/liuwenbohhh/p/4873873.html