Source for file DownloadResponse.php

Documentation is available at DownloadResponse.php

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