Created
July 28, 2017 13:45
-
-
Save fedek6/32a885a80483c5da5a6de3969c825dbd to your computer and use it in GitHub Desktop.
Dummy download tester for PHP with data throttling
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 | |
/** | |
* Long file download tester. | |
* You can test very long file downloads using PHP. | |
*/ | |
ob_start(); | |
/** | |
* Settings | |
*/ | |
/** @var string $fileName **/ | |
$fileName = 'test.bin'; | |
/** @var integer $fileSize in bytes **/ | |
$fileSize = 1000000 * 1024; | |
/** @var integer $uploadLimitPublic Limit in kb/s **/ | |
$uploadLimit = 1024; | |
/** @var integer $currentSize i bytes **/ | |
$currentSize = 0; | |
// size of one loop | |
$s = round( $uploadLimit * 1024 ); | |
/** | |
* Helpers | |
*/ | |
/** | |
* Send header fo file downloading | |
* @link http://zinoui.com/blog/download-large-files-with-php | |
*/ | |
function sendHeaders($fileSize, $type, $name) | |
{ | |
header('Pragma: public'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); | |
header('Cache-Control: private', false); | |
header('Content-Transfer-Encoding: binary'); | |
header('Content-Disposition: attachment; filename="'.$name.'";'); | |
header('Content-Type: ' . $type); | |
header('Content-Length: ' . $fileSize ); | |
} | |
/** | |
* APP | |
*/ | |
// send browser headers | |
sendHeaders( $fileSize, 'application/octet-stream', $fileName ); | |
while ( $currentSize < $fileSize) { | |
// data buffer to return | |
$t = str_repeat( 'a', $s); | |
$currentSize += $s; | |
// Send the current part of the file to the browser | |
echo $t; | |
// Flush the content | |
ob_flush(); | |
flush(); | |
// Sleep one second | |
sleep(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment