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

yipeiwu_com6年前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检查字符串中是否有外链的方法

本文实例讲述了php检查字符串中是否有外链的方法。分享给大家供大家参考。具体实现方法如下: /** * is_external_link 检测字符串是否包含外链 * @param...

PHP正则表达式处理函数(PCRE 函数)实例小结

本文实例讲述了PHP正则表达式处理函数。分享给大家供大家参考,具体如下: 有时候在一些特定的业务场景中需要匹配,或者提取一些关键的信息,例如匹配网页中的一些链接, 提取一些数据时,可能会...

深入eAccelerator与memcached的区别详解

eAccelerator和memcached,是目前较为主流的两个可使用在PHP之中的缓存加速工具.eAccelerator专门为PHP开发,而memcached不仅仅用在PHP之中,其...

php实现源代码加密的方法

本文实例讲述了php实现源代码加密的方法。分享给大家供大家参考。具体实现方法如下: <?php function RandAbc($length=""){//返回随...

php 运行效率总结(提示程序速度)

1,在函数中,传递数组时 使用 return 比使用 global 要高效 比如 function userloginfo($usertemp){ $detail=explode("|"...