> 백엔드 개발 > PHP 튜토리얼 > PHP下载文件步骤实例教程

PHP下载文件步骤实例教程

WBOY
풀어 주다: 2016-06-13 12:03:03
원래의
1187명이 탐색했습니다.

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>?>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿