Home > Backend Development > PHP Tutorial > php做控制下载速度代码 怎么又生带宽,又省cpu 和内存?

php做控制下载速度代码 怎么又生带宽,又省cpu 和内存?

WBOY
Release: 2016-06-02 11:33:20
Original
954 people have browsed it

cpuphp下载

<code> <?php /* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */    /* set here a limit of downloading rate (e.g. 10.20 Kb/s) */   // $download_rate = 10.20;   $download_rate = 200;   set_time_limit(0);    $download_file = 'myfile.rar';      $target_file = 'PHP敏捷开发CodeIgniter框架.rar';    if(file_exists($download_file)){        /* headers */        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');        header('Cache-control: private');        header('Content-Type: application/octet-stream'); //HTTP Content-type ( 二进制流,不知道下载文件类型)        header('Content-Length: '.filesize($download_file));        header('Content-Disposition: filename='.$target_file);        /* flush content */        flush();        /* open file */        $fh = @fopen($download_file, 'r');        while(!feof($fh)){            /* send only current part of the file to browser */            print fread($fh, round($download_rate * 1024));            /* flush the content to the browser */            flush();            /* sleep for 1 sec */            sleep(1);        }        /* close file */        @fclose($fh);    }else{        die('Fatal error: the '.$download_file.' file does not exist!');    }    ?></code>
Copy after login

怎么又生带宽,又省cpu 和内存?
1.是不sleep 控制下载速度好呢,还是sleep一下省下带宽呢?
2,每次读取2进制多少个字节合适呢,还是越大越好,还是小点分多次读取好?

本人小白,先谢过各位大侠了。。

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template