PHP限制文件下载的速度

开发技术 作者: 2024-07-27 08:50:01
PHP限制文件下载的速度

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

<?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!');
}
?>

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_51599.html