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