您的位置:首页 > 编程语言 > PHP开发

php限制文件下载速度代码

2016-03-21 11:08 731 查看

php限制文件下载速度代码

<?php
/* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
/* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
$download_rate = 10.20;

$download_file = 'download-file.zip';
$target_file = 'target-file.zip';

if(file_exists($download_file)){
/* headers */
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-control: private');
header('Content-Type: application/octet-stream'); //HTTP Content-type ( 二进制流,不知道下载文件类型)
header('Content-Length: '.filesize($download_file));
header('Content-Disposition: filename='.$target_file);

/* flush content */
flush();

/* open file */
$fh = @fopen($download_file, 'r');
while(!feof($fh)){
/* send only current part of the file to browser */
print fread($fh, round($download_rate * 1024));
/* flush the content to the browser */
flush();
/* sleep for 1 sec */
sleep(1);
}
/* close file */
@fclose($fh);
}else{
die('Fatal error: the '.$download_file.' file does not exist!');
}
?>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: