Heim  >  Artikel  >  Backend-Entwicklung  >  PHP使用文件流下载文件方法

PHP使用文件流下载文件方法

WBOY
WBOYOriginal
2016-06-13 12:09:401228Durchsuche

PHP使用文件流下载文件方法

?

?

PHP使用文件流下载文件方法

?

?

<code class=" hljs ">1、flush — 刷新输出缓冲2、ob_clean — 清空(擦掉)输出缓冲区注:上面2个函数可以解决PHP下载文件内容乱码</code>


DEMO :

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php </span>    <span class="hljs-comment">//fname为要下载的文件名</span>    <span class="hljs-comment">//$fpath为下载文件所在文件夹,默认是downlod</span>    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">download</span><span class="hljs-params">(<span class="hljs-variable">$fname</span>,<span class="hljs-variable">$fpath</span>=<span class="hljs-string">"download/"</span>)</span>{</span>        <span class="hljs-comment">//避免中文文件名出现检测不到文件名的情况,进行转码utf-8->gbk</span>        <span class="hljs-variable">$filename</span>=iconv(<span class="hljs-string">'utf-8'</span>, <span class="hljs-string">'gb2312'</span>, <span class="hljs-variable">$fname</span>);        <span class="hljs-variable">$path</span>=<span class="hljs-variable">$fpath</span>.<span class="hljs-variable">$filename</span>;        <span class="hljs-keyword">if</span>(!file_exists(<span class="hljs-variable">$path</span>)){<span class="hljs-comment">//检测文件是否存在</span>            <span class="hljs-keyword">echo</span> <span class="hljs-string">"文件不存在!"</span>;            <span class="hljs-keyword">die</span>();        }        <span class="hljs-variable">$fp</span>=fopen(<span class="hljs-variable">$path</span>,<span class="hljs-string">'r'</span>);<span class="hljs-comment">//只读方式打开</span>        <span class="hljs-variable">$filesize</span>=filesize(<span class="hljs-variable">$path</span>);<span class="hljs-comment">//文件大小</span>        <span class="hljs-comment">//返回的文件(流形式)</span>        header(<span class="hljs-string">"Content-type: application/octet-stream"</span>);        <span class="hljs-comment">//按照字节大小返回</span>        header(<span class="hljs-string">"Accept-Ranges: bytes"</span>);        <span class="hljs-comment">//返回文件大小</span>        header(<span class="hljs-string">"Accept-Length: $filesize"</span>);        <span class="hljs-comment">//这里客户端的弹出对话框,对应的文件名</span>        header(<span class="hljs-string">"Content-Disposition: attachment; filename="</span>.<span class="hljs-variable">$filename</span>);        <span class="hljs-comment">//================重点====================</span>        ob_clean();        flush();        <span class="hljs-comment">//=================重点===================</span>        <span class="hljs-comment">//设置分流</span>        <span class="hljs-variable">$buffer</span>=<span class="hljs-number">1024</span>;        <span class="hljs-comment">//来个文件字节计数器</span>        <span class="hljs-variable">$count</span>=<span class="hljs-number">0</span>;        <span class="hljs-keyword">while</span>(!feof(<span class="hljs-variable">$fp</span>)&&(<span class="hljs-variable">$filesize</span>-<span class="hljs-variable">$count</span>><span class="hljs-number">0</span>)){            <span class="hljs-variable">$data</span>=fread(<span class="hljs-variable">$fp</span>,<span class="hljs-variable">$buffer</span>);            <span class="hljs-variable">$count</span>+=<span class="hljs-variable">$data</span>;<span class="hljs-comment">//计数</span>            <span class="hljs-keyword">echo</span> <span class="hljs-variable">$data</span>;<span class="hljs-comment">//传数据给浏览器端</span>        }        fclose(<span class="hljs-variable">$fp</span>);    }    download(<span class="hljs-string">"testfile.doc"</span>);<span class="hljs-preprocessor">?></span></span></span></code>
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