head-img Force's Blog

php 文件下载限速func封装

PHP

php 文件下载限速function封装, 例:


function file_download($file_path = NULL, $new_filename = NULL, $rate = 500) {

    if(!file_exists($file_path) || !is_file($file_path)){
        return false;
    }

    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($file_path));

    if(!empty($new_filename)) {
        header('Content-Disposition: filename='.$new_filename);
    }
    flush();

    $file = fopen($file_path,"r");
    while (!feof($file)){
        print fread($file,round(intval($rate)*1024));
        flush();
        sleep(1);
    }
    fclose($file);
}


$path = './hi.zip';
file_download($path, 'hi'.'_'.time().'.zip', 350);


点我评论
打赏本文
二维码


125

文章

14

标签

 访客统计  Update-******