Last active
November 17, 2016 04:32
-
-
Save nemoTyrant/dfe09ad6b404f32096470ed762fbce2e to your computer and use it in GitHub Desktop.
download large file through php with rate limitation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $local_file = 'test.mp4'; | |
| $download_file = 'name.zip'; | |
| $file = fopen($local_file, "r"); | |
| // set the download rate limit (=> 20,5 kb/s) | |
| $download_rate = 1000; | |
| $chunk_size = 468; | |
| if (file_exists($local_file) && is_file($local_file)) { | |
| header('Cache-control: private'); | |
| header('Content-Type: application/octet-stream'); | |
| header('Content-Length: ' . filesize($local_file)); | |
| header('Content-Disposition: filename=' . $download_file); | |
| flush(); | |
| $file = fopen($local_file, "r"); | |
| $times = floor($download_rate / $chunk_size); | |
| $remaining = $download_rate % $chunk_size; | |
| while (!feof($file)) { | |
| for ($i = 0; $i < $times; $i++) { | |
| echo fread($file, round($chunk_size * 1024)); | |
| flush(); | |
| } | |
| echo fread($file, round($remaining * 1024)); | |
| flush(); | |
| // sleep one second | |
| sleep(1); | |
| } | |
| fclose($file); | |
| file_put_contents('./ttt', memory_get_peak_usage() / (1024 * 1024)); | |
| } else { | |
| die('Error: The file ' . $local_file . ' does not exist!'); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment