首頁  >  文章  >  後端開發  >  php程式碼實現控製檔下載速度

php程式碼實現控製檔下載速度

墨辰丷
墨辰丷原創
2018-06-12 09:36:371689瀏覽

這篇文章主要介紹了php控製文件下載速度的方法,實例分析了php操作文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php控制文件下載速度的方法。具體實現方法如下:

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

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php實作上傳圖片客戶端和伺服器端的方法

PHP基於dir類別實作目錄遍歷刪除

php利用陣列填入下拉清單框

#

以上是php程式碼實現控製檔下載速度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn