Home > Article > Backend Development > How to control file download speed in php_PHP tutorial
This article mainly introduces the method of controlling file download speed in php, and analyzes the skills of php file operation with examples, which has certain reference value. , friends in need can refer to it
The example in this article describes the method of controlling file download speed in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
?
3 4 513 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<๐>/*<๐> <๐>* set here a limit of downloading rate (e.g. 10.20 Kb/s)<๐> <๐>*/<๐> <๐>$download_rate = 10.20;<๐> <๐>$download_file = 'download-file.zip';<๐> <๐>$target_file = 'target-file.zip';<๐> <๐>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');<๐> <๐>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!');<๐> <๐>}<๐> <๐>?> |