Limiting File Downloading Speed In PHP
February 12th, 2008 | by programming |The following code allows you to limit the speed of downloading files from your server.
// file to download $filename = ‘file-to-download.zip’; // filename visible for client $visible_name = ‘client-file-name.zip’; // setting the download rate limit (=> 36,6 kb/s) $download_rate = 36.6; // checking if file exists if(file_exists($filename) && is_file($filename)) { // send headers header(‘Cache-control: private’); header(‘Content-Type: application/octet-stream’); header(‘Content-Length: ‘.filesize($filename)); header(‘Content-Disposition: filename=’.$visible_name); // flush content flush(); // open file stream $file = fopen($filename, “r”); while(!feof($file)) { // send the current file part to the browser print fread($file, round($download_rate * 1024)); // flush the content to the browser flush(); // sleep one second sleep(1); } // close file stream fclose($file); } else { die(‘Error: The file ‘.$visible_name.‘ does not exist!’); }