我们公司用的是联通的宽带办公,经常有的童鞋一下载文件就造成其他所有人的电脑连网页都打不开。所以一听到有人要下载东西就咬牙切齿。本地办公还好,如果是网站服务器出现这样的问题,造成网页经常打不开,估计领导会大发雷霆。这里枫叶博客为大家分享几段用PHP限制下载速度的代码。
// 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!');
}
枫叶博客
本地办公还好,如果是网站服务器出现这样的问题,造成网页经常打不开,估计领导会大发雷霆。
这个是限制什么网速的?
超赞一个,,
发现个好网站,我勒个鸡动啊,点赞是必须滴。
加油,我会经常来滴,期待更新。
加油,我会经常来滴,期待更新。
还有这么好的网站,必须赞一个!
每个网站,都会有一些小收获,谢谢分享。
学习了,很不错滴说,
谢谢分享
绝对的资源帖