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

yipeiwu_com5年前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-FPM实现性能优化

简介: PHP-FPM 是一个 PHP FastCGI 管理器,一般 Nginx 上面跑 PHP 程序都会将 PHP 程序丢给 PHP-FPM 来解析。好了,就这样! PHP 5.4 开...

PHP flush 函数使用注意事项

ob_*系列函数, 是操作PHP本身的输出缓冲区. 所以, ob_flush是刷新PHP自身的缓冲区. 而flush, 严格来讲, 这个只有在PHP做为apache的Module(han...

php实现的mongoDB单例模式操作类

本文实例讲述了php实现的mongoDB单例模式操作类。分享给大家供大家参考,具体如下: 看了好多mongo类都不尽人意。最后发现根本不需要自己封装类。php mongo 的扩展自带的方...

php学习之数据类型之间的转换介绍

复制代码 代码如下: /*数据类型之间相互转换 * 一种是强制转换 * setType(变量,类型); //int,integer,float,double等等。 * 这个函数将原变量的...

PHP setcookie指定domain参数后,在IE下设置cookie失效的解决方法

下面的代码:在chrome和firefox下可以生成三个cookie: cookie[one]、cookie[two]、cookie[three] 在IE下,只能生成cookie[one...