PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Application/Responses/DownloadResponse.php

https://github.com/DocX/nette
PHP | 112 lines | 66 code | 14 blank | 32 comment | 1 complexity | a94b13330d400a972b21301893eaa9a0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Application
  17. */
  18. /*namespace Nette\Application;*/
  19. require_once dirname(__FILE__) . '/../../Object.php';
  20. require_once dirname(__FILE__) . '/../../Application/IPresenterResponse.php';
  21. /**
  22. * File download response.
  23. *
  24. * @author David Grudl
  25. * @copyright Copyright (c) 2004, 2009 David Grudl
  26. * @package Nette\Application
  27. */
  28. class DownloadResponse extends /*Nette\*/Object implements IPresenterResponse
  29. {
  30. /** @var string */
  31. private $file;
  32. /** @var string */
  33. private $contentType;
  34. /** @var string */
  35. private $name;
  36. /**
  37. * @param string file path
  38. * @param string user name name
  39. * @param string MIME content type
  40. */
  41. public function __construct($file, $name = NULL, $contentType = NULL)
  42. {
  43. if (!is_file($file)) {
  44. throw new BadRequestException("File '$file' doesn't exist.");
  45. }
  46. $this->file = $file;
  47. $this->name = $name ? $name : basename($file);
  48. $this->contentType = $contentType ? $contentType : 'application/octet-stream';
  49. }
  50. /**
  51. * Returns the path to an downloaded file.
  52. * @return string
  53. */
  54. final public function getFile()
  55. {
  56. return $this->file;
  57. }
  58. /**
  59. * Returns the file name.
  60. * @return string
  61. */
  62. final public function getName()
  63. {
  64. return $this->name;
  65. }
  66. /**
  67. * Returns the MIME content type of an downloaded file.
  68. * @return string
  69. */
  70. final public function getContentType()
  71. {
  72. return $this->contentType;
  73. }
  74. /**
  75. * Sends response to output.
  76. * @return void
  77. */
  78. public function send()
  79. {
  80. /*Nette\*/Environment::getHttpResponse()->setContentType($this->contentType);
  81. /*Nette\*/Environment::getHttpResponse()->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');
  82. readfile($this->file);
  83. }
  84. }