Maison  >  Article  >  php教程  >  php控制文件下载速度

php控制文件下载速度

大家讲道理
大家讲道理original
2016-11-11 10:31:141518parcourir

<?php
    /* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
    $download_rate = 10.20;
       
    $download_file = &#39;download-file.zip&#39;; 
    $target_file = &#39;target-file.zip&#39;;
       
    if(file_exists($download_file)){
        /* headers */
        header(&#39;Last-Modified: &#39;.gmdate(&#39;D, d M Y H:i:s&#39;).&#39; GMT&#39;);
        header(&#39;Cache-control: private&#39;);
        header(&#39;Content-Type: application/octet-stream&#39;);
        header(&#39;Content-Length: &#39;.filesize($download_file));
        header(&#39;Content-Disposition: filename=&#39;.$target_file);
       
        /* flush content */
        flush();
       
        /* open file */
        $fh = @fopen($download_file, &#39;r&#39;);
        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(&#39;Fatal error: the &#39;.$download_file.&#39; file does not exist!&#39;);
    }
    ?>

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:PHP 计算页面执行时间Article suivant:java生成html万年历