php 文件下载速度限制的实现方法

内容摘要
这篇文章主要为大家详细介绍了php 文件下载速度限制的实现方法,具有一定的参考价值,可以用来参考一下。

php限制文件下载速度,这个方案可能有一些缺陷,但可以参考一下,php限制
文章正文

这篇文章主要为大家详细介绍了php 文件下载速度限制的实现方法,具有一定的参考价值,可以用来参考一下。

php限制文件下载速度,这个方案可能有一些缺陷,但可以参考一下,php限制文件下载速度,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<code class="php">
/**
 * 限制文件下载速度
 *
 * @param
 * @arrange (www.idcnote.com)
 **/
// local file that should be send to the client
$local_file = 'test-file.zip';
  
// filename that the user gets as default
$download_file = 'your-download-name.zip';
  
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
  
if(file_exists($local_file) && is_file($local_file)) {
  
    // send headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);
  
    // flush content
    flush();
  
    // open file stream
    $file = fopen($local_file, "r");
  
    while (!feof($file)) {
  
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
  
        // flush the content to the browser
        flush();
  
        // sleep one second
        sleep(1);
    }
  
    // close file stream
    fclose($file);
  
}
else {
    die('Error: The file '.$local_file.' does not exist!');
}
 
 
/***   代码来自php教程(www.idcnote.com)   ***/</code>

注:关于php 文件下载速度限制的实现方法的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!