1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Upload;
15
16 17 18 19 20 21
22
23 class Uploader
24 {
25 26 27 28 29
30 public $files;
31
32 33 34 35 36
37
38 public $mode = 0755;
39
40 41 42
43 public $lang = 'EN';
44
45 46 47 48 49
50 public $fileExit;
51
52 53 54 55 56
57 public $saveName;
58
59 60 61 62 63
64 public $saveURL;
65
66 67 68 69 70
71
72 public $savePath;
73
74 75 76 77 78
79 public $message;
80
81 82 83 84 85 86 87
88
89 public $randomLength = 'Ymd';
90
91 92 93 94 95 96 97 98 99
100
101 public $randomNameType = 1;
102
103 104 105 106 107
108
109 public $timezone = 'PRC';
110
111 112 113 114 115
116
117 public $formats = ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp'];
118
119 120 121 122 123
124
125 public $maxSize = 1024;
126
127 128 129 130
131 public $cover = true;
132
133 134 135 136 137
138 public $redirect = false;
139
140 141 142 143 144
145
146 public $redirectURL = "";
147
148 149 150 151 152
153 public $errors = [
154 'empty' => 'The upload file can\'t be empty.',
155 'format' => 'The uploaded file format does not conform to the regulations.',
156 'maxsize' => 'The upload file size too large.',
157 'unwritable' => 'Save the directory not to write, please change permissions.',
158 'not_exist' => 'Save the directory not exist.',
159 'same_file' => 'There are already the same file exist.'
160 ];
161
162 163 164 165 166 167 168
169
170 public function __construct(array $configs)
171 {
172 $this->config($configs);
173 }
174
175 176 177 178 179 180 181
182
183 public function config(array $configs)
184 {
185 foreach($configs as $key => $value)
186 {
187 if (property_exists($this, $key))
188 {
189 $this->$key = $value;
190 }
191 }
192 }
193
194 195 196 197 198 199 200
201
202 public function upload($name)
203 {
204
205
206 if ( empty($_FILES[$name]['name']) )
207 {
208 $this->error($this->errors['empty'], 0, true);
209
210 return false;
211 }
212
213 $this->files = $_FILES[$name];
214
215
216 if( !file_exists($this->savePath) )
217 {
218 $this->error($this->errors['not_exist'], 0, true);
219
220 return false;
221 }
222
223
224 if( !is_writable($this->savePath) )
225 {
226 $this->error($this->errors['unwritable'], 0, true);
227
228 return false;
229 }
230
231 return $this->moveFile();
232 }
233
234 235 236 237 238 239
240
241 private function moveFile()
242 {
243 $this->setSeveName();
244
245 $files = $this->files;
246
247 if ($this->formats != "" && !in_array($this->fileExt, $this->formats))
248 {
249 $formats = implode(',', $this->formats);
250 $message = "Your upload file " . $files["name"] . " is " . $this->fileExt;
251 $message .= " format, The system is not allowed to upload, you can only upload " . $formats . " format's file.";
252
253 $this->error($message, 0, true);
254
255 return false;
256 }
257
258 if ($files["size"] / 1024 > $this->maxSize)
259 {
260 $message = "Your upload file " . $files["name"] . " The file size exceeds of the system limit size " . $this->maxSize . " KB.";
261 $this->error($message, 0, true);
262
263 return false;
264 }
265
266
267 if (!$this->cover)
268 {
269
270 if (file_exists($this->savePath . $this->saveName))
271 {
272 $this->error($this->saveName . $this->errors['same_file'], 0, true);
273
274 return false;
275 }
276 }
277
278 if ( !@move_uploaded_file( $files["tmp_name"], iconv("utf-8", "gbk", $this->savePath . $this->saveName) ) )
279 {
280 switch ($files["error"])
281 {
282 case '0':
283 $message = "File upload successfully.";
284 break;
285
286 case '1':
287 $message = "The uploaded file exceeds the value of the upload_max_filesize option in php.ini.";
288 break;
289
290 case '2':
291 $message = "The size of the upload file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.";
292 break;
293
294 case '3':
295 $message = "Only part of the file is uploaded.";
296 break;
297
298 case '4':
299 $message = "No file is uploaded.";
300 break;
301
302 case '6':
303 $message = "Can't find upload temp directory.";
304 break;
305
306 case '7':
307 $message = "Error writing file to hard drive";
308 break;
309
310 case '8':
311 $message = "An extension has stopped the upload of the file.";
312 break;
313
314 case '999':
315 default:
316 $message = "Unknown error, please check the file is damaged, whether the oversized and other reasons.";
317 break;
318 }
319
320 $this->error($message, 0, true);
321
322 return false;
323 }
324
325 @unlink($files["tmp_name"]);
326
327 return true;
328 }
329
330 331 332 333 334 335
336
337 private function randomFileName()
338 {
339 $fileName = '';
340
341
342 if ($this->randomNameType == 1)
343 {
344 date_default_timezone_set($this->timezone);
345
346 $date = date($this->randomLength);
347 echo $dir = $this->savePath . $date;
348
349 if ( !file_exists($dir) ) {
350 mkdir($dir, $this->mode, true);
351 }
352
353 $fileName = $date . '/' . time();
354 }
355 elseif ($this->randomNameType == 2)
356 {
357 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
358 $max = strlen($chars) - 1;
359 mt_srand( (double) microtime() * 1000000);
360
361 for ($i = 0; $i < $this->randomLength; $i++)
362 {
363 $fileName .= $chars[mt_rand(0, $max)];
364 }
365 }
366 else
367 {
368 }
369
370 $this->fileExt = $this->getFileExt($this->files["name"]);
371
372 $fileName = $fileName . '.' . $this->fileExt;
373
374 return $fileName;
375 }
376
377 378 379 380 381 382
383
384 private function setSeveName()
385 {
386 $this->saveName = $this->randomFileName();
387
388 if ($this->saveName == '')
389 {
390 $this->saveName = $this->files['name'];
391 }
392 }
393
394 395 396 397 398 399
400
401 public function getSeveName()
402 {
403 return $this->saveName;
404 }
405
406 407 408 409 410 411 412
413
414 public function getFileExt($fileName)
415 {
416 return trim( strtolower( substr( strrchr($fileName, '.'), 1) ) );
417 }
418
419 420 421 422 423 424
425
426 public function redirect()
427 {
428 header('location: ' . $this->redirectURL);
429 }
430
431 432 433 434 435 436 437 438 439
440
441 public function message($message, $success = 0, $return = false)
442 {
443 $array = array(
444 'success' => $success,
445 'message' => $message
446 );
447
448 $url = $this->saveURL . $this->saveName;
449
450
451 if ($this->redirect)
452 {
453 $this->redirectURL .= '&success=' . $success . '&message=' . $message;
454
455 if ($success == 1)
456 {
457 $this->redirectURL .= '&url=' . $url;
458 }
459
460 $this->redirect();
461 }
462 else
463 {
464 echo "success =>" . $success;
465 if ($success == 1)
466 {
467 $array['url'] = $url;
468 }
469
470 $this->message = $array = json_encode($array);
471
472 if ($return)
473 {
474 return $array;
475 }
476 else
477 {
478 echo $array;
479 }
480 }
481 }
482
483 484 485 486 487
488
489 public function jsonHeader()
490 {
491 header('Content-Type: application/json');
492 }
493
494 495 496 497 498 499 500
501
502 public function success($message = "Upload successfully.", $return = false)
503 {
504 return $this->message($message, 1, $return);
505 }
506
507 508 509 510 511 512 513
514
515 public function error($message = "Upload failed.", $return = false)
516 {
517 return $this->message($message, 0, $return);
518 }
519
520 521 522 523 524
525
526 public function errorExit()
527 {
528 exit;
529 }
530 }