span style="color: #0000BB"><?phpclass download { public $properties = array('old_name' => '', 'new_name' => '', 'type' => '', 'size' => '', 'resume' => '', 'max_speed' => ''); public $range = 0; public function download($path, $name, $resume = 0, $max_speed = 0) { $name = ($name == '') ? substr(strrchr('/' . $path, '/'), 1) : $name; $name = explode('/', $name); $name = end($name); $file_size = $this->getFilesize($path); $this->properties = array('old_name' => $path, 'new_name' => $name, 'type' => "application/force-download", 'size' => $file_size, 'resume' => $resume, 'max_speed' => $max_speed); if ($this->properties['resume']) { if (isset($_SERVER['HTTP_RANGE'])) { $this->range = $_SERVER['HTTP_RANGE']; $this->range = str_replace("bytes=", '', $this->range); $this->range = str_replace("-", '', $this->range); } else { $this->range = 0; } if ($this->range > $this->properties['size']) $this->range = 0; } else { $this->range = 0; } } public function download_file() { $ctype_arr = array('mp3' => 'audio/mp3'); ($this->range) ? header($_SERVER['SERVER_PROTOCOL'] . ' 206 Partial Content') : header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:"); header("Cache-Control: public"); header("Content-Description: File Transfer"); $pathinfo = pathinfo($this->properties['old_name']); $ras = $pathinfo['extension']; (array_key_exists($ras, $ctype_arr)) ? header("Content-Type: " . $ctype_arr["$ras"]) : header("Content-Type: " . $this->properties["type"]); header('Content-Disposition: attachment; filename="' . $this->properties['new_name'] . '";'); header("Content-Transfer-Encoding: binary"); if ($this->properties['resume']) header("Accept-Ranges: bytes"); if ($this->range) { header("Content-Range: bytes {$this->range}-" . ($this->properties['size'] - 1) . '/' . $this->properties['size']); header("Content-Length: " . ($this->properties['size'] - $this->range)); } else { header("Content-Length: " . $this->properties['size']); } @ini_set('max_execution_time', 0); @set_time_limit(); $this->_download($this->properties['old_name'], $this->range); } public function _download($filename, $range = 0) { @ob_end_clean(); if (($speed = $this->properties['max_speed']) > 0) $sleep_time = (8 / $speed) * 1e6; else $sleep_time = 0; $handle = fopen($filename, 'rb'); fseek($handle, $range); if ($handle === false) { return false; } while (!feof($handle)) { print (fread($handle, 1024 * 8)); ob_flush(); flush(); usleep($sleep_time); } fclose($handle); return true; } /** * Функция подсчета размера файла */ public function getFilesize($url) { if(file_exists($url)) { return filesize($url); } else { $x = array_change_key_case(get_headers($url, 1), CASE_LOWER); return (strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0) ? $x['content-length'][1] : $x['content-length']; } } } ?> |