How can you limit download speed in a PHP script?
To limit download speed in a PHP script, you can use the `flush()` and `usleep()` functions to control the rate at which data is sent to the client. By sending data in small chunks with delays in between, you can effectively limit the download speed.
<?php
$file = 'path_to_your_file.ext';
$chunkSize = 1024; // 1KB chunks
$delay = 50000; // 50 milliseconds delay between chunks
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
$handle = fopen($file, 'rb');
while (!feof($handle)) {
echo fread($handle, $chunkSize);
flush();
usleep($delay);
}
fclose($handle);
?>
Keywords
Related Questions
- How can the display and functionality of modals be optimized when using PHP for web development?
- What best practices should be followed when iterating over CSV data in PHP to ensure proper handling of data and avoid errors like undefined array keys?
- How can the use of backticks in MySQL queries improve code stability and prevent errors in PHP applications?