Skip to content

Instantly share code, notes, and snippets.

@fedek6
Created July 28, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedek6/32a885a80483c5da5a6de3969c825dbd to your computer and use it in GitHub Desktop.
Save fedek6/32a885a80483c5da5a6de3969c825dbd to your computer and use it in GitHub Desktop.
Dummy download tester for PHP with data throttling
<?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