PHP控制文件下载速度

// 将发送到客户端的本地文件
$local_file = 'test-file.zip';
// 文件名
$download_file = 'your-download-name.zip';
// 设置下载速率(=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)) {
    // 发送 headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);
    // flush 内容
    flush();
    // 打开文件流
    $file = fopen($local_file, "r");
    while (!feof($file)) {
        // 发送当前部分文件给浏览者
        print fread($file, round($download_rate * 1024));
        // flush 内容输出到浏览器端
        flush();
        ob_flush();  //防止PHP或web服务器的缓存机制影响输出
        // 终端1秒后继续
        sleep(1);
    }
    // 关闭文件流
    fclose($file);
}
else {
    die('Error: 文件 '.$local_file.' 不存在!');
}

Comments : 0

有问题可在下面发表评论,当然没事也可以在下面吹吹牛皮、扯扯淡!

发表评论

*


Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/blog/content/templates/Bitter/footer.php:40) in /www/wwwroot/blog/include/lib/view.php on line 23