/
UploadHelper.php
381 lines (313 loc) · 9.13 KB
/
UploadHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Simple class for files upload
*
* @author Rafael Wendel Pinheiro
* @version 1.0
* @link https://github.com/rafaelwendel/UploadHelper
*/
class Upload_Helper {
/**
* The file to be sent
* @access protected
* @var array
*/
protected $_file;
/**
* The extension of file
* @access protected
* @var String
*/
protected $_ext;
/**
* The folder to receive the file
* @access protected
* @var String
*/
protected $_uploads_folder;
/**
* The uploaded file path
* @access protected
* @var String
*/
protected $_file_path;
/**
* The new name of file
* @access protected
* @var String
*/
protected $_file_name = null;
/**
* The allowed extensions in the upload
* @access protected
* @var array
*/
protected $_allowed_exts = array();
/**
* The max size of file (in MegaBytes)
* @access protected
* @var Double
*/
protected $_max_size = 2;
/**
* Overwrite file with same name?
* @access protected
* @var Boolean
*/
protected $_overwrite = true;
/**
* The error message
* @access protected
* @var String
*/
protected $_error;
/**
* The default messages
* @access protected
* @var array
*/
protected $_default_messages = array();
/**
* The language of messages. (English (en) or Portuguese (pt)
* @access protected
* @var String
*/
protected $_language = 'en';
/**
* Constructor method. You can define the file, uploads folder, new file name and the language. Define too the default messages
* @access public
* @param $file File The file to be upload
* @param $uploads_folder String The path of the receive folder
* @param $file_name String The new name
* @return Void
*/
public function __construct($file = '', $uploads_folder = '', $file_name = '', $language = 'en') {
if (isset ($file)){
$this->set_file($file);
}
if (isset ($uploads_folder)){
$this->set_uploads_folder($uploads_folder);
}
if (isset ($file_name)){
$this->set_file_name($file_name);
}
$this->set_language($language);
$this->set_default_messages();
}
/**
* Set the default messages
* @access protected
* @return Void
*/
protected function set_default_messages(){
$this->_default_messages['en'] = array(
'1' => 'File is not set',
'2' => 'Uploads folder is not set or no exists',
'3' => 'Files of type {{exts}} are not allowed',
'4' => 'The file size is larger than {{max_size}}MB',
'5' => 'Error when uploading'
);
$this->_default_messages['pt'] = array(
'1' => 'Arquivo não setado',
'2' => 'Pasta de uploads não definida ou não existe',
'3' => 'Arquivos do tipo {{exts}} não são permitidos',
'4' => 'O tamanho do arquivo é maior que {{max_size}}MB',
'5' => 'Erro ao fazer upload'
);
}
/**
* Set the language messages
* @access public
* @param $language String The language
* @return Void
*/
public function set_language($language){
$this->_language = ($language == 'en' || $language == 'pt' ? $language : 'en');
}
/**
* Set a file
* @access public
* @param $file File The file to be upload
* @return Void
*/
public function set_file($file){
$this->_file = $file;
$this->set_ext($file);
}
/**
* Set the extension of file
* @access protected
* @param $file File The file will be sent
* @return Void
*/
protected function set_ext($file){
$this->_ext = strtolower(end(explode('.', $file['name'])));
}
/**
* Set the folder to receive the file
* @access public
* @param $uploads_folder String The path of the receive folder
* @return Void
*/
public function set_uploads_folder($uploads_folder){
if (substr ($uploads_folder, -1) == '/'){
$this->_uploads_folder = $uploads_folder;
}
else{
$this->_uploads_folder = $uploads_folder . '/';
}
}
/**
* Set the new name of the file
* @access public
* @param $file_name String The new name
* @return Void
*/
public function set_file_name($file_name){
$this->_file_name = $file_name;
}
/**
* Set the max size of file
* @access public
* @param $max_size Double The max size of file
* @return Void
*/
public function set_max_size($max_size){
$this->_max_size = $max_size;
}
/**
* Set the allowed extensions in the upload
* @access public
* @param $file_name String The new name
* @return Void
*/
public function set_allowed_exts($allowed_exts){
if (is_array ($allowed_exts)){
$this->_allowed_exts = $allowed_exts;
}
if (is_string($allowed_exts)){
$this->_allowed_exts[] = $allowed_exts;
}
}
/**
* Overwrite file with same name? (true or false)
* @access public
* @param $param Boolean Yes(true) or no(false)
* @return Void
*/
public function set_overwrite($param){
$this->_overwrite = (bool) $param;
}
/**
* Set a error message
* @access protected
* @param $error_num String The error number (1, 2, 3 ou 4)
* @return Void
*/
protected function set_error($error_num){
$this->_error = $this->_default_messages[$this->_language][$error_num];
}
/**
* Get the error message
* @access public
* @return String The error message
*/
public function get_error(){
$this->_error = str_replace('{{exts}}', $this->_ext, $this->_error);
$this->_error = str_replace('{{max_size}}', $this->_max_size, $this->_error);
return $this->_error;
}
/**
* Get the uploaded file path
* @access public
* @return String The uploaded file path
*/
public function get_file_path(){
return $this->_file_path;
}
/**
* Keep the file with the same name
* @access protected
* @return Void
*/
protected function some_name(){
$tmp_name = explode('.', $this->_file['name']);
unset($tmp_name[count($tmp_name) - 1]);
$this->_file_name = implode('.', $tmp_name);
}
/**
* Checks whether to overwrite files with the same name. If not, creates name incremented ($name, $name_1, $name_2, $name_n)
* @access protected
* @return Void
*/
protected function verify_overwrite(){
if (!$this->_overwrite){
$tmp_name = $this->_file_name;
$x = 1;
while (file_exists($this->_uploads_folder . $this->_file_name . '.' . $this->_ext)){
$this->_file_name = $tmp_name . '_' . $x;
$x++;
}
}
}
/**
* Validates requirements for uploading
* @access protected
* @return Boolean True if valid
*/
protected function is_valid(){
if (empty ($this->_file['name'])){
$this->set_error(1);
return false;
}
if (empty ($this->_uploads_folder) || !file_exists($this->_uploads_folder)){
$this->set_error(2);
return false;
}
if (!in_array ($this->_ext, $this->_allowed_exts)){
$this->set_error(3);
return false;
}
if (!$this->validate_size()){
$this->set_error(4);
return false;
}
return true;
}
/**
* Validate the size of file
* @access protected
* @return Boolean True if valid
*/
protected function validate_size(){
$file_size = $this->_file['size'];
/* Convert bytes to megabytes */
$file_size = ($file_size / 1024) / 1024;
if ($file_size > $this->_max_size){
return false;
}
return true;
}
/**
* Upload the file
* @access public
* @return Boolean True if file has been uploaded
*/
public function upload_file(){
if (!$this->is_valid()){
return false;
}
if (!isset ($this->_file_name)){
$this->some_name();
}
$this->verify_overwrite();
if (move_uploaded_file ($this->_file['tmp_name'], $this->_uploads_folder . $this->_file_name . '.' . $this->_ext)){
$this->_file_path = $this->_uploads_folder . $this->_file_name . '.' . $this->_ext;
return true;
}
else{
$this->set_error(5);
return false;
}
}
}