PHP code to control file download speed

墨辰丷
Release: 2023-03-31 17:20:02
Original
1744 people have browsed it

This article mainly introduces the method of controlling file download speed in PHP. It analyzes the skills of PHP file operation with examples. It has certain reference value. Friends in need can refer to it.

The examples in this article describe PHP control. File download speed method. The specific implementation method is as follows:

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

php methods to implement client-side and server-side uploading of images

PHP implements directory traversal and deletion based on dir class

php uses an array to populate the drop-down list box

The above is the detailed content of PHP code to control file download speed. For more information, please follow other related articles on the PHP Chinese website!

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