Rumah > pembangunan bahagian belakang > tutorial php > PHP下载文件步骤实例教程

PHP下载文件步骤实例教程

WBOY
Lepaskan: 2016-06-13 12:03:03
asal
1188 orang telah melayarinya

PHP下载文件方法实例教程

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

转自:http://www.cnblogs.com/cfinder010/p/3197195.html

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

方法一:

php
<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008000;">/*</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008000;">** 下载文件* header函数*</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008000;">*/</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Description: File Transfer'<span style="margin: 0px; padding: 0px; line-height: 1.8;">);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Type: application/octet-stream'<span style="margin: 0px; padding: 0px; line-height: 1.8;">);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Disposition: attachment; filename='.<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">basename</span>(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$filepath</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">));</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Transfer-Encoding: binary'<span style="margin: 0px; padding: 0px; line-height: 1.8;">);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('<span style="margin: 0px; padding: 0px; line-height: 1.8;">Expires: 0′);header(</span>'Cache-Control: must-revalidate, post-check=0, pre-check=0<span style="margin: 0px; padding: 0px; line-height: 1.8;">′);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Pragma: public'<span style="margin: 0px; padding: 0px; line-height: 1.8;">);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Length: ' . <span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">filesize</span>(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$filepath</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">));</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">readfile</span>(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$file_path</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">);</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 style="margin: 0px; padding: 0px; line-height: 1.8;">php</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008000;">//</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008000;">文件下载//readfile</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$fileinfo</span> = <span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">pathinfo</span>(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$filename</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-type: application/x-'.<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$fileinfo</span>['extension'<span style="margin: 0px; padding: 0px; line-height: 1.8;">]);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Disposition: attachment; filename='.<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$fileinfo</span>['basename'<span style="margin: 0px; padding: 0px; line-height: 1.8;">]);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">header</span>('Content-Length: '.<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">filesize</span>(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$filename</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">));</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #008080;">readfile</span>(<span style="margin: 0px; padding: 0px; line-height: 1.8; color: #800080;">$thefile</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">);</span><span style="margin: 0px; padding: 0px; line-height: 1.8; color: #0000ff;">exit</span><span style="margin: 0px; padding: 0px; line-height: 1.8;">();</span>?>
Salin selepas log masuk
Label berkaitan:
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