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  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Application
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * File download response.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Application
  20. 20:  */
  21. 21: class DownloadResponse extends Object implements IPresenterResponse
  22. 22: {
  23. 23:     /** @var string */
  24. 24:     private $file;
  25. 25:  
  26. 26:     /** @var string */
  27. 27:     private $contentType;
  28. 28:  
  29. 29:     /** @var string */
  30. 30:     private $name;
  31. 31:  
  32. 32:  
  33. 33:  
  34. 34:     /**
  35. 35:      * @param  string  file path
  36. 36:      * @param  string  user name name
  37. 37:      * @param  string  MIME content type
  38. 38:      */
  39. 39:     public function __construct($file$name NULL$contentType NULL)
  40. 40:     {
  41. 41:         if (!is_file($file)) {
  42. 42:             throw new BadRequestException("File '$file' doesn't exist.");
  43. 43:         }
  44. 44:  
  45. 45:         $this->file $file;
  46. 46:         $this->name $name $name basename($file);
  47. 47:         $this->contentType $contentType $contentType 'application/octet-stream';
  48. 48:     }
  49. 49:  
  50. 50:  
  51. 51:  
  52. 52:     /**
  53. 53:      * Returns the path to a downloaded file.
  54. 54:      * @return string 
  55. 55:      */
  56. 56:     final public function getFile()
  57. 57:     {
  58. 58:         return $this->file;
  59. 59:     }
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Returns the file name.
  65. 65:      * @return string 
  66. 66:      */
  67. 67:     final public function getName()
  68. 68:     {
  69. 69:         return $this->name;
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Returns the MIME content type of a downloaded file.
  76. 76:      * @return string 
  77. 77:      */
  78. 78:     final public function getContentType()
  79. 79:     {
  80. 80:         return $this->contentType;
  81. 81:     }
  82. 82:  
  83. 83:  
  84. 84:  
  85. 85:     /**
  86. 86:      * Sends response to output.
  87. 87:      * @return void 
  88. 88:      */
  89. 89:     public function send()
  90. 90:     {
  91. 91:         Environment::getHttpResponse()->setContentType($this->contentType);
  92. 92:         Environment::getHttpResponse()->setHeader('Content-Disposition''attachment; filename="' $this->name '"');
  93. 93:         readfile($this->file);
  94. 94:     }
  95. 95:  
  96. 96: }