1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think\response; |
14
|
|
|
|
15
|
|
|
use think\Exception; |
16
|
|
|
use think\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* File Response |
20
|
|
|
*/ |
21
|
|
|
class File extends Response |
22
|
|
|
{ |
23
|
|
|
protected $expire = 360; |
24
|
|
|
protected $name; |
25
|
|
|
protected $mimeType; |
26
|
|
|
protected $isContent = false; |
27
|
|
|
protected $force = true; |
28
|
|
|
|
29
|
|
|
public function __construct($data = '', int $code = 200) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->init($data, $code); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 处理数据 |
36
|
|
|
* @access protected |
37
|
|
|
* @param mixed $data 要处理的数据 |
38
|
|
|
* @return mixed |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
protected function output($data) |
42
|
|
|
{ |
43
|
|
|
if (!$this->isContent && !is_file($data)) { |
44
|
|
|
throw new Exception('file not exists:' . $data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
ob_end_clean(); |
48
|
|
|
|
49
|
|
|
if (!empty($this->name)) { |
50
|
|
|
$name = $this->name; |
51
|
|
|
} else { |
52
|
|
|
$name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($this->isContent) { |
56
|
|
|
$mimeType = $this->mimeType; |
57
|
|
|
$size = strlen($data); |
58
|
|
|
} else { |
59
|
|
|
$mimeType = $this->getMimeType($data); |
60
|
|
|
$size = filesize($data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->header['Pragma'] = 'public'; |
64
|
|
|
$this->header['Content-Type'] = $mimeType ?: 'application/octet-stream'; |
65
|
|
|
$this->header['Cache-control'] = 'max-age=' . $this->expire; |
66
|
|
|
$this->header['Content-Disposition'] = ($this->force ? 'attachment; ' : '') . 'filename="' . $name . '"'; |
67
|
|
|
$this->header['Content-Length'] = $size; |
68
|
|
|
$this->header['Content-Transfer-Encoding'] = 'binary'; |
69
|
|
|
$this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT'; |
70
|
|
|
|
71
|
|
|
$this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
72
|
|
|
|
73
|
|
|
return $this->isContent ? $data : file_get_contents($data); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* 设置是否为内容 必须配合mimeType方法使用 |
78
|
|
|
* @access public |
79
|
|
|
* @param bool $content |
|
|
|
|
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function isContent(bool $content = true) |
83
|
|
|
{ |
84
|
|
|
$this->isContent = $content; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* 设置有效期 |
90
|
|
|
* @access public |
91
|
|
|
* @param integer $expire 有效期 |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function expire(int $expire) |
95
|
|
|
{ |
96
|
|
|
$this->expire = $expire; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
|
|
|
|
101
|
|
|
* 设置文件类型 |
102
|
|
|
* @access public |
103
|
|
|
* @param string $filename 文件名 |
|
|
|
|
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function mimeType(string $mimeType) |
107
|
|
|
{ |
108
|
|
|
$this->mimeType = $mimeType; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 设置文件强制下载 |
114
|
|
|
* @access public |
115
|
|
|
* @param bool $force 强制浏览器下载 |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function force(bool $force) |
119
|
|
|
{ |
120
|
|
|
$this->force = $force; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 获取文件类型信息 |
125
|
|
|
* @access public |
126
|
|
|
* @param string $filename 文件名 |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function getMimeType(string $filename): string |
130
|
|
|
{ |
131
|
|
|
if (!empty($this->mimeType)) { |
132
|
|
|
return $this->mimeType; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
136
|
|
|
|
137
|
|
|
return finfo_file($finfo, $filename); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 设置下载文件的显示名称 |
142
|
|
|
* @access public |
143
|
|
|
* @param string $filename 文件名 |
|
|
|
|
144
|
|
|
* @param bool $extension 后缀自动识别 |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function name(string $filename, bool $extension = true) |
148
|
|
|
{ |
149
|
|
|
$this->name = $filename; |
150
|
|
|
|
151
|
|
|
if ($extension && false === strpos($filename, '.')) { |
152
|
|
|
$this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|