|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for organize uploads storage |
|
4
|
|
|
* |
|
5
|
|
|
* @file UploadFile.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2018 Alexander Yancharuk |
|
11
|
|
|
* @date 2013-07-27 22:06 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Tools; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class UploadFile |
|
20
|
|
|
* |
|
21
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
22
|
|
|
*/ |
|
23
|
|
|
class UploadFile extends File |
|
24
|
|
|
{ |
|
25
|
|
|
protected $tmp_path; |
|
26
|
|
|
protected $hash = ''; |
|
27
|
|
|
protected $sub_dir; |
|
28
|
|
|
protected $orig_name; |
|
29
|
|
|
protected $www_path; |
|
30
|
|
|
protected $dir_mask = 0755; |
|
31
|
|
|
protected $hash_algorithm = 'sha1'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Method created for test purpose |
|
35
|
|
|
* |
|
36
|
|
|
* @codeCoverageIgnore |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $filename File name |
|
39
|
|
|
* @param string $destination Save destination |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function moveUploadedFile($filename, $destination) |
|
44
|
|
|
{ |
|
45
|
|
|
return move_uploaded_file($filename, $destination); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set upload directory mask |
|
50
|
|
|
* |
|
51
|
|
|
* @param int $dir_mask Value must be octal. Examples: 0755, 02755 |
|
52
|
|
|
* |
|
53
|
|
|
* @return UploadFile |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function setDirMask($dir_mask) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->dir_mask = $dir_mask; |
|
58
|
|
|
|
|
59
|
2 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Det upload directory mask |
|
64
|
|
|
* |
|
65
|
|
|
* @return int |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function getDirMask() |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->dir_mask; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get uploaded file origin name |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function getOrigName() |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->orig_name; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set uploaded file origin name |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $orig_name Origin file name |
|
86
|
|
|
* |
|
87
|
|
|
* @return UploadFile |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function setOrigName($orig_name) |
|
90
|
|
|
{ |
|
91
|
2 |
|
$this->orig_name = $orig_name; |
|
92
|
|
|
|
|
93
|
2 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Generate path for uploaded file |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function initStorageName() |
|
100
|
|
|
{ |
|
101
|
|
|
// initialize storage name only once |
|
102
|
2 |
|
if ('' !== $this->getHash()) { |
|
103
|
2 |
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
$array = explode('.', $this->getOrigName()); |
|
107
|
2 |
|
$extension = strtolower(end($array)); |
|
108
|
|
|
|
|
109
|
2 |
|
$this->setHash(hash_file($this->getHashAlgorithm(), $this->getTmpPath())) |
|
110
|
2 |
|
->setSubDir(substr($this->getHash(), 0, 2)) |
|
111
|
2 |
|
->setName(substr($this->getHash(), 2) . '.' . $extension) |
|
112
|
2 |
|
->setWwwPath( |
|
113
|
2 |
|
str_replace($_SERVER['DOCUMENT_ROOT'], '', $this->getDir()) |
|
114
|
2 |
|
. DIRECTORY_SEPARATOR . $this->getSubDir() . DIRECTORY_SEPARATOR |
|
115
|
2 |
|
. $this->getName() |
|
116
|
|
|
) |
|
117
|
2 |
|
->setPath( |
|
118
|
2 |
|
$this->getDir() . DIRECTORY_SEPARATOR . $this->getSubDir() |
|
119
|
2 |
|
. DIRECTORY_SEPARATOR . $this->getName() |
|
120
|
|
|
); |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get uploaded file name hash |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function getHash() |
|
129
|
|
|
{ |
|
130
|
2 |
|
return $this->hash; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set uploaded file name hash |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $hash |
|
137
|
|
|
* |
|
138
|
|
|
* @return UploadFile |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function setHash($hash) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$this->hash = $hash; |
|
143
|
|
|
|
|
144
|
2 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get uploaded file sub-dir |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function getSubDir() |
|
153
|
|
|
{ |
|
154
|
2 |
|
return $this->sub_dir; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Set uploaded file sub-dir |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $sub_dir |
|
161
|
|
|
* |
|
162
|
|
|
* @return UploadFile |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function setSubDir($sub_dir) |
|
165
|
|
|
{ |
|
166
|
2 |
|
$this->sub_dir = $sub_dir; |
|
167
|
|
|
|
|
168
|
2 |
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Save uploaded file |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
8 |
|
public function save() |
|
177
|
|
|
{ |
|
178
|
8 |
|
$dir = $this->getDir(); |
|
179
|
|
|
|
|
180
|
8 |
|
is_dir($dir) || mkdir($dir, $this->getDirMask(), true); |
|
181
|
8 |
|
is_writable($dir) || chmod($dir, $this->getDirMask()); |
|
182
|
|
|
|
|
183
|
8 |
|
return file_exists($this->getPath()) |
|
184
|
1 |
|
? true |
|
185
|
8 |
|
: $this->moveUploadedFile($this->getTmpPath(), $this->getPath()); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get uploaded file temporary path |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
2 |
|
public function getTmpPath() |
|
194
|
|
|
{ |
|
195
|
2 |
|
return $this->tmp_path; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Set uploaded file temporary path |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $tmp_path Uploaded file temp path |
|
202
|
|
|
* |
|
203
|
|
|
* @return UploadFile |
|
204
|
|
|
*/ |
|
205
|
2 |
|
public function setTmpPath($tmp_path) |
|
206
|
|
|
{ |
|
207
|
2 |
|
$this->tmp_path = $tmp_path; |
|
208
|
|
|
|
|
209
|
2 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Get uploaded file www-path |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public function getWwwPath() |
|
218
|
|
|
{ |
|
219
|
2 |
|
return $this->www_path; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Set uploaded file www-path |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $www_path www-path to file |
|
226
|
|
|
* |
|
227
|
|
|
* @return UploadFile |
|
228
|
|
|
*/ |
|
229
|
2 |
|
public function setWwwPath($www_path) |
|
230
|
|
|
{ |
|
231
|
2 |
|
$this->www_path = $www_path; |
|
232
|
|
|
|
|
233
|
2 |
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get uploaded file name hashing algorithm |
|
238
|
|
|
* |
|
239
|
|
|
* @see hash_algos() |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
2 |
|
public function getHashAlgorithm() |
|
243
|
|
|
{ |
|
244
|
2 |
|
return $this->hash_algorithm; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Set uploaded file name hashing algorithm |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $hash_algorithm Hash algorithm |
|
251
|
|
|
* |
|
252
|
|
|
* @see hash_algos() |
|
253
|
|
|
* @return UploadFile |
|
254
|
|
|
*/ |
|
255
|
2 |
|
public function setHashAlgorithm($hash_algorithm) |
|
256
|
|
|
{ |
|
257
|
2 |
|
$this->hash_algorithm = $hash_algorithm; |
|
258
|
|
|
|
|
259
|
2 |
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|