欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-10 15:25:52
方法一:复制代码 代码如下: header('content-description: file transfer'); header('content...
方法一:
复制代码 代码如下:

 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();