1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpSyst\Uploader; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class SimpSyst Uploader |
7
|
|
|
* |
8
|
|
|
* @author Diego Matos <https://github.com/diegoamatos> |
9
|
|
|
* @package SimpSyst\Uploader |
10
|
|
|
*/ |
11
|
|
|
abstract class Uploader |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $path; |
15
|
|
|
|
16
|
|
|
/** @var resource */ |
17
|
|
|
protected $file; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $ext; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected static $allowTypes = []; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
protected static $extensions = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $uploadDir |
33
|
|
|
* @param string $fileTypeDir |
34
|
|
|
* @param bool $monthYearPath |
35
|
|
|
* @example $u = new Upload("storage/uploads", "images"); |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $uploadDir, string $fileTypeDir, bool $monthYearPath = true) |
38
|
|
|
{ |
39
|
|
|
$this->dir($uploadDir); |
40
|
|
|
$this->dir("{$uploadDir}/{$fileTypeDir}"); |
41
|
|
|
$this->path = "{$uploadDir}/{$fileTypeDir}"; |
42
|
|
|
|
43
|
|
|
if ($monthYearPath) { |
44
|
|
|
$this->path("{$uploadDir}/{$fileTypeDir}"); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public static function isAllowed(): array |
52
|
|
|
{ |
53
|
|
|
return static::$allowTypes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public static function isExtension(): array |
60
|
|
|
{ |
61
|
|
|
return static::$extensions; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function name(string $name): string |
69
|
|
|
{ |
70
|
|
|
$name = filter_var(mb_strtolower($name), FILTER_SANITIZE_STRIPPED); |
71
|
|
|
$formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª'; |
72
|
|
|
$replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr '; |
73
|
|
|
$name = str_replace(["-----", "----", "---", "--"], "-", |
74
|
|
|
str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace)))); |
75
|
|
|
|
76
|
|
|
$this->name = "{$name}." . $this->ext; |
77
|
|
|
|
78
|
|
|
if (file_exists("{$this->path}/{$this->name}") && is_file("{$this->path}/{$this->name}")) { |
79
|
|
|
$this->name = "{$name}-" . time() . ".{$this->ext}"; |
80
|
|
|
} |
81
|
|
|
return $this->name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $dir |
86
|
|
|
* @param int $mode |
87
|
|
|
*/ |
88
|
|
|
protected function dir(string $dir, int $mode = 0755): void |
89
|
|
|
{ |
90
|
|
|
if (!file_exists($dir) || !is_dir($dir)) { |
91
|
|
|
mkdir($dir, $mode, true); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $path |
97
|
|
|
*/ |
98
|
|
|
protected function path(string $path): void |
99
|
|
|
{ |
100
|
|
|
list($yearPath, $mothPath) = explode("/", date("Y/m")); |
101
|
|
|
|
102
|
|
|
$this->dir("{$path}/{$yearPath}"); |
103
|
|
|
$this->dir("{$path}/{$yearPath}/{$mothPath}"); |
104
|
|
|
$this->path = "{$path}/{$yearPath}/{$mothPath}"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $inputName |
109
|
|
|
* @param $files |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function multiple($inputName, $files): array |
113
|
|
|
{ |
114
|
|
|
$gbFiles = []; |
115
|
|
|
$gbCount = count($files[$inputName]["name"]); |
116
|
|
|
$gbKeys = array_keys($files[$inputName]); |
117
|
|
|
|
118
|
|
|
for ($gbLoop = 0; $gbLoop < $gbCount; $gbLoop++): |
119
|
|
|
foreach ($gbKeys as $key): |
120
|
|
|
$gbFiles[$gbLoop][$key] = $files[$inputName][$key][$gbLoop]; |
121
|
|
|
endforeach; |
122
|
|
|
endfor; |
123
|
|
|
|
124
|
|
|
return $gbFiles; |
125
|
|
|
} |
126
|
|
|
} |