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