解析PHP实现下载文件的两种方法

前端之家收集整理的这篇文章主要介绍了解析PHP实现下载文件的两种方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法一:

代码如下:
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);

方法二:

代码如下:
$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();

猜你在找的PHP相关文章