コード例 #1
0
	/**
	 * Sends response to output.
	 * @return void
	 */
	public function send(Nette\Web\IHttpRequest $httpRequest, Nette\Web\IHttpResponse $httpResponse)
	{
		$httpResponse->setContentType($this->contentType);
		$httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');

		$filesize = $length = filesize($this->file);
		$handle = fopen($this->file, 'r');

		if ($this->resuming) {
			$httpResponse->setHeader('Accept-Ranges', 'bytes');
			$range = $httpRequest->getHeader('Range');
			if ($range !== NULL) {
				$range = substr($range, 6); // 6 == strlen('bytes=')
				list($start, $end) = explode('-', $range);
				if ($start == NULL) {
					$start = 0;
				}
				if ($end == NULL) {
					$end = $filesize - 1;
				}

				if ($start < 0 || $end <= $start || $end > $filesize -1) {
					$httpResponse->setCode(416); // requested range not satisfiable
					return;
				}

				$httpResponse->setCode(206);
				$httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
				$length = $end - $start + 1;
				fseek($handle, $start);

			} else {
				$httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
			}
		}

		$httpResponse->setHeader('Content-Length', $length);
		while (!feof($handle)) {
			echo fread($handle, 4e6);
		}
		fclose($handle);
	}