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: */
11:
12: namespace Nette\Application;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * File download response.
20: *
21: * @author David Grudl
22: */
23: class DownloadResponse extends Nette\Object implements IPresenterResponse
24: {
25: /** @var string */
26: private $file;
27:
28: /** @var string */
29: private $contentType;
30:
31: /** @var string */
32: private $name;
33:
34:
35:
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:
47: $this->file = $file;
48: $this->name = $name ? $name : basename($file);
49: $this->contentType = $contentType ? $contentType : 'application/octet-stream';
50: }
51:
52:
53:
54: /**
55: * Returns the path to a downloaded file.
56: * @return string
57: */
58: final public function getFile()
59: {
60: return $this->file;
61: }
62:
63:
64:
65: /**
66: * Returns the file name.
67: * @return string
68: */
69: final public function getName()
70: {
71: return $this->name;
72: }
73:
74:
75:
76: /**
77: * Returns the MIME content type of a downloaded file.
78: * @return string
79: */
80: final public function getContentType()
81: {
82: return $this->contentType;
83: }
84:
85:
86:
87: /**
88: * Sends response to output.
89: * @return void
90: */
91: public function send()
92: {
93: Nette\Environment::getHttpResponse()->setContentType($this->contentType);
94: Nette\Environment::getHttpResponse()->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"');
95: readfile($this->file);
96: }
97:
98: }
99: