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);


点我评论
打赏本文
二维码


103

文章

24

标签

 访客统计  Update-1201-05:00

 网站信息