Heim  >  Artikel  >  Backend-Entwicklung  >  PHP下载文件步骤实例教程

PHP下载文件步骤实例教程

WBOY
WBOYOriginal
2016-06-13 12:03:031173Durchsuche

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>?>

以上代码用到了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>?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn