分享---PHP下载文件的两种方法

巴扎黑
发布: 2016-11-12 10:46:42
原创
1397 人浏览过

PHP下载文件的两种方法与代码。

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

方法一:

<?php
/*** 下载文件* header函数**/header(&#39;Content-Description: File Transfer&#39;);
header(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename=&#39;.basename($filepath));
header(&#39;Content-Transfer-Encoding: binary&#39;);
header(&#39;Expires: 0′);header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(&#39;Pragma: public&#39;);
header(&#39;Content-Length: &#39; . filesize($filepath));
readfile($file_path);
?>
登录后复制

以上代码用到了php header函数,可以参考以下如下的文章:
php header()函数的简单例子
php header函数实现文件下载的实例代码
php中header函数的用法举例详解
php header 使用详解
php header函数 文件下载时直接提示保存的代码
php header函数实现文本文件下载的方法
php 文件头部(header)信息详解
php使用header发送各种类型文件下载的例子

了解php中header函数的用法。

方法二:

<?php//文件下载//readfile
$fileinfo = pathinfo($filename);
header(&#39;Content-type: application/x-&#39;.$fileinfo[&#39;extension&#39;]);
header(&#39;Content-Disposition: attachment; filename=&#39;.$fileinfo[&#39;basename&#39;]);
header(&#39;Content-Length: &#39;.filesize($filename));
readfile($thefile);exit();
?>
登录后复制


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!