When you are doing O & M, you will often encounter a problem, that is, someone downloads something in the office, affecting Internet access and office work. If the same problem occurs on the server, it is estimated that the boss will get angry and things will get worse ...... Today, I would like to recommend several lines of code to limit the network speed of PHP. I hope to help you.
[Code][PHP] Code view sourcePrint?01
// local file that should be send to the client
02
$local_file
=
'test-file.zip'
;
03
04
// filename that the user gets as default
05
$download_file
=
'your-download-name.zip'
;
06
07
// set the download rate limit (=> 20,5 kb/s)
08
$download_rate
= 20.5;
09
10
if
(
file_exists
(
$local_file
) &&
is_file
(
$local_file
)) {
11
12
// send headers
13
header(
'Cache-control: private'
);
14
header(
'Content-Type: application/octet-stream'
);
15
header(
'Content-Length: '
.
filesize
(
$local_file
));
16
header(
'Content-Disposition: filename='
.
$download_file
);
17
18
// flush content
19
flush
();
20
21
// open file stream
22
$file
=
fopen
(
$local_file
,
"r"
);
23
24
while
(!
feof
(
$file
)) {
25
26
// send the current file part to the browser
27
print
fread
(
$file
,
round
(
$download_rate
* 1024));
28
29
// flush the content to the browser
30
flush
();
31
32
// sleep one second
33
sleep(1);
34
}
35
36
// close file stream
37
fclose(
$file
);
38
39
}
40
41
else
{
42
die
(
'Error: The file '
.
$local_file
.
' does not exist!'
);
43
}