Mark V
1: <?php
2: namespace MOC\V\Core\FileSystem\Vendor\Universal;
3:
4: use MOC\V\Core\FileSystem\FileSystem;
5:
6: /**
7: * Class Download
8: *
9: * @package MOC\V\Core\FileSystem\Vendor\Universal
10: */
11: class Download
12: {
13:
14: /** @var \MOC\V\Core\FileSystem\Component\IBridgeInterface $Location */
15: private $Location = null;
16: /** @var null|string $Name */
17: private $Name = null;
18:
19: /**
20: * @param string $Location
21: * @param null|string $Name
22: */
23: public function __construct($Location, $Name = null)
24: {
25:
26: $this->setLocation($Location);
27: $this->Name = $Name;
28: }
29:
30: /**
31: * @return bool|string
32: */
33: public function __toString()
34: {
35:
36: if ($this->getRealPath()) {
37: if (function_exists('mime_content_type')) {
38: $Type = mime_content_type($this->getRealPath());
39: } else {
40: if (function_exists('finfo_file')) {
41: $Handler = finfo_open(FILEINFO_MIME);
42: $Type = finfo_file($Handler, $this->getRealPath());
43: finfo_close($Handler);
44: } else {
45: $Type = "application/force-download";
46: }
47: }
48:
49: // Set headers.
50: header('Content-Description: Download');
51: header('Content-Type: '.$Type);
52: header('Content-Disposition: attachment; filename="'.( $this->Name ? $this->Name : basename($this->getRealPath()) ).'"');
53: header('Content-Transfer-Encoding: binary');
54: header('Expires: 0');
55: header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
56: header('Pragma: public');
57: header('Content-Length: '.filesize($this->getRealPath()));
58: header('Connection: close');
59:
60: // Erase and flush the output buffer
61: if (ob_get_level()) {
62: ob_clean();
63: flush();
64: }
65:
66: return file_get_contents($this->getRealPath());
67: } else {
68: // Set headers.
69: header('HTTP/1.0 404 Not Found');
70:
71: // Erase and flush the output buffer
72: if (ob_get_level()) {
73: ob_clean();
74: flush();
75: }
76:
77: return '';
78: }
79: }
80:
81: /**
82: * @return string
83: */
84: public function getRealPath()
85: {
86:
87: return $this->Location->getRealPath();
88: }
89:
90: /**
91: * @return string
92: */
93: public function getLocation()
94: {
95:
96: return $this->Location->getLocation();
97: }
98:
99: /**
100: * @param string $Location
101: */
102: public function setLocation($Location)
103: {
104:
105: $this->Location = FileSystem::getFileLoader($Location);
106: }
107: }
108:
109: