php 文件下载速度限制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 文件下载速度限制相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">// local file that should be send to the client</span>
<span style="color: #0000ff;">$local_file</span> = <span style="color: #ff0000;">'test-file.zip'</span>;
<span style="color: #808080; font-style: italic;">// filename that the user gets as default</span>
<span style="color: #0000ff;">$download_file</span> = <span style="color: #ff0000;">'your-download-name.zip'</span>;
<span style="color: #808080; font-style: italic;">// set the download rate limit (=> 20,5 kb/s)</span>
<span style="color: #0000ff;">$download_rate</span> = <span style="color: #cc66cc;">20.5</span>;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><a href="http://www.php.net/file_exists"><span style="color: #000066;">file_exists</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$local_file</span><span style="color: #66cc66;">)</span> && <a href="http://www.php.net/is_file"><span style="color: #000066;">is_file</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$local_file</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// send headers</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Cache-control: private'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Type: application/octet-stream'</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Length: '</span>.<a href="http://www.php.net/filesize"><span style="color: #000066;">filesize</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$local_file</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Content-Disposition: filename='</span>.<span style="color: #0000ff;">$download_file</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// flush content</span>
<a href="http://www.php.net/flush"><span style="color: #000066;">flush</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// open file stream</span>
<span style="color: #0000ff;">$file</span> = <a href="http://www.php.net/fopen"><span style="color: #000066;">fopen</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$local_file</span>, <span style="color: #ff0000;">"r"</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span>!<a href="http://www.php.net/feof"><span style="color: #000066;">feof</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// send the current file part to the browser</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <a href="http://www.php.net/fread"><span style="color: #000066;">fread</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$file</span>, <a href="http://www.php.net/round"><span style="color: #000066;">round</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$download_rate</span> * <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// flush the content to the browser</span>
<a href="http://www.php.net/flush"><span style="color: #000066;">flush</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// sleep one second</span>
<a href="http://www.php.net/sleep"><span style="color: #000066;">sleep</span></a><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">// close file stream</span>
<a href="http://www.php.net/fclose"><span style="color: #000066;">fclose</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Error: The file '</span>.<span style="color: #0000ff;">$local_file</span>.<span style="color: #ff0000;">' does not exist!'</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
以上是关于php 文件下载速度限制的主要内容,如果未能解决你的问题,请参考以下文章