/** Transmit a file for downloading by HTTP client; If kilobytes per second is specified, output is throttled (bandwidth will not be controlled by default); Return TRUE if successful, FALSE otherwise @param $_file string @param $_kbps integer @public **/ public static function send($_file, $_kbps = 0) { $_file = F3::resolve($_file); if (!file_exists($_file)) { F3::http404(); return FALSE; } if (PHP_SAPI != 'cli' && !F3::$global['QUIET'] && !headers_sent()) { header(F3::HTTP_Content . ': application/octet-stream'); header(F3::HTTP_Disposition . ': ' . 'attachment; filename=' . basename($_file)); header(F3::HTTP_Length . ': ' . filesize($_file)); F3::httpCache(0); ob_end_flush(); } $_max = ini_get('max_execution_time'); $_ctr = 1; $_handle = fopen($_file, 'r'); $_time = time(); while (!feof($_handle) && !connection_aborted()) { if ($_kbps > 0) { // Throttle bandwidth $_ctr++; $_elapsed = microtime(TRUE) - $_time; if ($_ctr / $_kbps > $_elapsed) { usleep(1000000.0 * ($_ctr / $_kbps - $_elapsed)); } } // Send 1KiB and reset timer echo fread($_handle, 1024); set_time_limit($_max); } fclose($_handle); return TRUE; }