search
Homephp教程php手册PHP:如何限制文件下载速度

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

Jun 06, 2016 pm 08:09 PM
phpdownloadhowDownload Documentsystemspeedlimit

下载系统呢,很多人都应该接触过,很简单的一个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.’ 不存在!’);   
}  

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools