Home > php教程 > php手册 > body text

PHP:如何限制文件下载速度

WBOY
Release: 2016-06-06 20:09:18
Original
1086 people have browsed it

下载系统呢,很多人都应该接触过,很简单的一个PHP应用嘛。不过呢,谈到PHP限速下载,估计就有些人摸不着头脑了。其实呢,这个也很简单的哦。下面就着一个简单的PHP下载功能结合限制速度给出一段代码。但愿对初学者有些许帮助哦: // 本地文件 $local_file =

下载系统呢,很多人都应该接触过,很简单的一个PHP应用嘛。不过呢,谈到PHP限速下载,估计就有些人摸不着头脑了。其实呢,这个也很简单的哦。下面就着一个简单的PHP下载功能结合限制速度给出一段代码。但愿对初学者有些许帮助哦:

// 本地文件   
$local_file = ’test-file.zip’;    
// 下载是设置的默认名称   
$download_file = ’your-download-name.zip’;    
// 限制下载速度在20.5KB/s   
$download_rate = 20.5;   
//检测文件是否存在 并且保证文件有效    
if(file_exists($local_file) && is_file($local_file)) {    
    //发送头部标识   
    header(‘Cache-control: private‘);   
    header(‘Content-Type: application/octet-stream’);   
    header(‘Content-Length: ’.filesize($local_file));   
    header(‘Content-Disposition: filename=’.$download_file);    
    //刷新输出缓冲   
    flush();    
    //打开文件   
    $file = fopen($local_file, “r”);    
    while (!feof($file)) {    
        // 发送文件喽   
        print fread($file, round($download_rate * 1024));    
        // 缓冲到浏览器   
        flush();    
        //暂停1秒执行   
        sleep(1);   
    }    
    //关闭文件流   
    fclose($file);   
}   
else {   
    die(‘错误: 文件——’.$local_file.’ 不存在!’);   
}  
Copy after login

转载请注明:IT路人 » PHP:如何限制文件下载速度

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!