/Nette/Application/Responses/DownloadResponse.php
PHP | 112 lines | 66 code | 14 blank | 32 comment | 1 complexity | a94b13330d400a972b21301893eaa9a0 MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- /**
- * Nette Framework
- *
- * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "Nette license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://nettephp.com
- *
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @license http://nettephp.com/license Nette license
- * @link http://nettephp.com
- * @category Nette
- * @package Nette\Application
- */
- /*namespace Nette\Application;*/
- require_once dirname(__FILE__) . '/../../Object.php';
- require_once dirname(__FILE__) . '/../../Application/IPresenterResponse.php';
- /**
- * File download response.
- *
- * @author David Grudl
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @package Nette\Application
- */
- class DownloadResponse extends /*Nette\*/Object implements IPresenterResponse
- {
- /** @var string */
- private $file;
- /** @var string */
- private $contentType;
- /** @var string */
- private $name;
- /**
- * @param string file path
- * @param string user name name
- * @param string MIME content type
- */
- public function __construct($file, $name = NULL, $contentType = NULL)
- {
- if (!is_file($file)) {
- throw new BadRequestException("File '$file' doesn't exist.");
- }
- $this->file = $file;
- $this->name = $name ? $name : basename($file);
- $this->contentType = $contentType ? $contentType : 'application/octet-stream';
- }
- /**
- * Returns the path to an downloaded file.
- * @return string
- */
- final public function getFile()
- {
- return $this->file;
- }
- /**
- * Returns the file name.
- * @return string
- */
- final public function getName()
- {
- return $this->name;
- }
- /**
- * Returns the MIME content type of an downloaded file.
- * @return string
- */
- final public function getContentType()
- {
- return $this->contentType;
- }
- /**
- * Sends response to output.
- * @return void
- */
- public function send()
- {
- /*Nette\*/Environment::getHttpResponse()->setContentType($this->contentType);
- /*Nette\*/Environment::getHttpResponse()->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');
- readfile($this->file);
- }
- }