function send($file, $mime = NULL, $kbps = 0, $force = TRUE, $name = NULL, $flush = TRUE)
{
if (!is_file($file)) {
return FALSE;
}
$size = filesize($file);
if (PHP_SAPI != 'cli') {
header('Content-Type: ' . ($mime ?: $this->mime($file)));
if ($force) {
header('Content-Disposition: attachment; ' . 'filename="' . ($name !== NULL ? $name : basename($file)) . '"');
}
header('Accept-Ranges: bytes');
header('Content-Length: ' . $size);
header('X-Powered-By: ' . Base::instance()->get('PACKAGE'));
}
$ctr = 0;
$handle = fopen($file, 'rb');
$start = microtime(TRUE);
while (!feof($handle) && ($info = stream_get_meta_data($handle)) && !$info['timed_out'] && !connection_aborted()) {
if ($kbps) {
// Throttle output
$ctr++;
if ($ctr / $kbps > ($elapsed = microtime(TRUE) - $start)) {
usleep(1000000.0 * ($ctr / $kbps - $elapsed));
}
}
// Send 1KiB and reset timer
echo fread($handle, 1024);
if ($flush) {
ob_flush();
flush();
}
}
fclose($handle);
return $size;
}