PHP code to limit file download speed, _PHP tutorial

WBOY
Release: 2016-07-12 09:07:05
Original
814 people have browsed it

php code to limit file download speed,

Sometimes you will ask to slow down the file download speed for some purpose, for example, you want to achieve file download progress function. The biggest benefit of limiting download speed is to save bandwidth and avoid network congestion caused by excessive instantaneous traffic. This article will share with you how to limit the download speed of files through PHP code.
First, let’s take a look at the code that uses php to limit file download speed:

<&#63;php 
// 将发送到客户端的本地文件 
$local_file='abc.zip'; 
// 文件名 
$download_file='your-download-name.zip'; 
// 设置下载速率(=> 31.2 kb/s) 
$download_rate=31.2; 
if(file_exists($local_file)&&is_file($local_file)){ 
 header('Cache-control: private');// 发送 headers 
 header('Content-Type: application/octet-stream'); 
 header('Content-Length: '.filesize($local_file)); 
 header('Content-Disposition: filename='.$download_file); 
 flush();// 刷新内容 
 $file=fopen($local_file,"r"); 
 while (!feof($file)){ 
  print fread($file,round($download_rate*1024));// 发送当前部分文件给浏览者 
  flush();// flush 内容输出到浏览器端 
  sleep(1);// 终端1秒后继续 
 } 
 fclose($file);// 关闭文件流 
}else{ 
 die('Error: 文件 '.$local_file.' 不存在!'); 
} 
Copy after login

Let’s do some analysis on the above code:
1. Limit the file download speed to 31.2kb/s, that is, only send a 20.5kb file stream to the client per second until the entire file is sent. If there is no such limit, then the files will be sent to the client together in the form of a stream, and as many as there are sent, What will happen? If the file size is 2m, then transmitting the 2m data stream all at once may cause network congestion and interrupt the execution of the script. This download method cannot be used in actual applications.
2. Technical aspects , first add a header file, declare Content-Type as application/octet-stream, indicating that the request will be sent as a stream, and declare Content-Length, which declares the size of the file stream . Flush() is used in the code. The flush function is to refresh the buffer of the php program and realize dynamic output of print.

Another reminder: Cleverly using the above code, you can also realize the function of the client displaying the file download progress bar. Friends who are interested can try it, but I won’t write more here.

The above has implemented the function of limiting file download speed in PHP. I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1063234.htmlTechArticlephp code to limit file download speed. Sometimes you will require the speed of downloading files for some purpose. Slow it down a bit, for example if you want to implement a file download progress bar function. Limit download speed...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!