Rumah > pembangunan bahagian belakang > tutorial php > PHP下载文件的两种方法与代码分享_PHP教程

PHP下载文件的两种方法与代码分享_PHP教程

WBOY
Lepaskan: 2016-07-20 11:13:07
asal
970 orang telah melayarinya

方法一:

<?<span php
</span><span /*</span><span *
* 下载文件
* header函数
*
</span><span */</span>
<span header</span>('Content-Description: File Transfer'<span );

</span><span header</span>('Content-Type: application/octet-stream'<span );
</span><span header</span>('Content-Disposition: attachment; filename='.<span basename</span>(<span $filepath</span><span ));
</span><span header</span>('Content-Transfer-Encoding: binary'<span );
</span><span header</span>('<span Expires: 0&prime;);
header(</span>'Cache-Control: must-revalidate, post-check=0, pre-check=0<span &prime;);
</span><span header</span>('Pragma: public'<span );
</span><span header</span>('Content-Length: ' . <span filesize</span>(<span $filepath</span><span ));
</span><span readfile</span>(<span $file_path</span><span );
</span>?>
Salin selepas log masuk

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

了解php中header函数的用法。

方法二:

<?<span php
</span><span //</span><span 文件下载
//readfile</span>
<span $fileinfo</span> = <span pathinfo</span>(<span $filename</span><span );
</span><span header</span>('Content-type: application/x-'.<span $fileinfo</span>['extension'<span ]);
</span><span header</span>('Content-Disposition: attachment; filename='.<span $fileinfo</span>['basename'<span ]);
</span><span header</span>('Content-Length: '.<span filesize</span>(<span $filename</span><span ));
</span><span readfile</span>(<span $thefile</span><span );
</span><span exit</span><span ();
</span>?>
Salin selepas log masuk

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440394.htmlTechArticle方法一: ? ('Content-Description: File Transfer' ('Content-Type: application/octet-stream' ('Content-Disposition: attachment; filename='. ( ('Content-Transfer-Encoding: binary'...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan