Skip to content

Instantly share code, notes, and snippets.

@flxxyz
Last active August 28, 2018 16:15
Show Gist options
  • Save flxxyz/b1276e030dd73be5c5bc55bbec81d8b7 to your computer and use it in GitHub Desktop.
Save flxxyz/b1276e030dd73be5c5bc55bbec81d8b7 to your computer and use it in GitHub Desktop.
限制文件读取速度
<?php
function limit_speed_print_file($filename, $limit_rate = 100)
{
if (file_exists($filename) && is_file($filename)) {
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.get_safe_filesize($filename));
header('Content-Disposition: attachment; filename='
.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: public');
$chunk = round($limit_rate * 1024);
$f = fopen($filename, 'rb');
if ($f === false) {
print '打开文件失败';
exit(1);
}
while (!feof($f)) {
print fread($f, $chunk);
flush();
sleep(1);
}
fclose($f);
} else {
die('Error: The file '.$filename.' does not exist!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment