1 | <?php |
||
12 | class Uploader |
||
13 | { |
||
14 | /** |
||
15 | * Whether to overwrite if there is a file with the same name |
||
16 | * @var boolean |
||
17 | */ |
||
18 | protected $override = false; |
||
19 | |||
20 | /** |
||
21 | * The save path of the uploaded files |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $savePath = './'; |
||
25 | |||
26 | /** |
||
27 | * Whether to enable random file name |
||
28 | * @var boolean |
||
29 | */ |
||
30 | protected $isRandName = false; |
||
31 | |||
32 | /** |
||
33 | * The rules collection |
||
34 | * @var RuleInterface[] |
||
35 | */ |
||
36 | protected $rules = []; |
||
37 | |||
38 | /** |
||
39 | * File name generator |
||
40 | * @var callable |
||
41 | */ |
||
42 | protected $filenameGenerator; |
||
43 | |||
44 | public function __construct($path = './') |
||
49 | |||
50 | /** |
||
51 | * Sets override mode |
||
52 | * @param boolean $override |
||
53 | */ |
||
54 | public function setOverride($override) |
||
58 | |||
59 | /** |
||
60 | * Gets whether to enabled override mode |
||
61 | * @return boolean |
||
62 | */ |
||
63 | public function getOverride() |
||
67 | |||
68 | /** |
||
69 | * Set save path for uploaded files |
||
70 | * @param string $path |
||
71 | * @throws UploadException |
||
72 | */ |
||
73 | public function setSavePath($path) |
||
83 | |||
84 | /** |
||
85 | * Get the save path of uploaded files |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getSavePath() |
||
92 | |||
93 | /** |
||
94 | * Sets whether to enable rand-name mode |
||
95 | * @param $result |
||
96 | */ |
||
97 | public function setRandName($result) |
||
101 | |||
102 | /** |
||
103 | * Sets whether to enable rand-name mode |
||
104 | * @param boolean $result |
||
105 | * @deprecated Use "setRandName" instead |
||
106 | */ |
||
107 | public function setIsRandName($result) |
||
111 | |||
112 | /** |
||
113 | * Checks whether the rand-name mode is used |
||
114 | * @return boolean |
||
115 | */ |
||
116 | public function isRandName() |
||
120 | |||
121 | /** |
||
122 | * Checks whether the rand-name mode is used |
||
123 | * @return boolean |
||
124 | * @deprecated Use "isRandName" instead |
||
125 | */ |
||
126 | public function getIsRandName() |
||
130 | |||
131 | /** |
||
132 | * Set filename generator |
||
133 | * @param callable $generator |
||
134 | */ |
||
135 | public function setFilenameGenerator(callable $generator) |
||
139 | |||
140 | /** |
||
141 | * Gets the current filename generator |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getFilenameGenerator() |
||
152 | |||
153 | /** |
||
154 | * Add a rule |
||
155 | * @param RuleInterface $rule |
||
156 | */ |
||
157 | public function addRule(RuleInterface $rule) |
||
161 | |||
162 | /** |
||
163 | * Gets all rules |
||
164 | * @return array |
||
165 | */ |
||
166 | public function getRules() |
||
170 | |||
171 | /** |
||
172 | * Process upload |
||
173 | * @param array $files |
||
174 | * @throws UploadException |
||
175 | * @return FileInfo|FileInfo[]; |
||
176 | */ |
||
177 | public function process($files) |
||
200 | |||
201 | /** |
||
202 | * Process the uploaded file |
||
203 | * @param array $info |
||
204 | * @return FileInfo; |
||
205 | */ |
||
206 | protected function processUpload(array $info) |
||
221 | |||
222 | /** |
||
223 | * Validates the uploaded file |
||
224 | * @param FileInfo $file |
||
225 | * @return boolean |
||
226 | */ |
||
227 | protected function validateUploadFile(FileInfo $file) |
||
238 | |||
239 | /** |
||
240 | * Moves the uploaded file |
||
241 | * Illegal upload files and other unsolicited causes can not be moved to throw an exception |
||
242 | * @param FileInfo $file |
||
243 | * @param string $newFilePath |
||
244 | * @return boolean |
||
245 | * @throws UploadException |
||
246 | */ |
||
247 | protected function moveUploadedFile(FileInfo $file, $newFilePath) |
||
264 | |||
265 | /** |
||
266 | * Makes default rand filename generator |
||
267 | * @return callable |
||
268 | */ |
||
269 | protected function makeDefaultRandFilenameGenerator() |
||
275 | |||
276 | /** |
||
277 | * Makes default origin filename generator |
||
278 | * @return callable |
||
279 | */ |
||
280 | protected function makeDefaultOriginFilenameGenerator() |
||
286 | |||
287 | /** |
||
288 | * Generates an new file path |
||
289 | * @param FileInfo $file |
||
290 | * @return string |
||
291 | */ |
||
292 | protected function generateFilename(FileInfo $file) |
||
298 | } |
||
299 |
If you suppress an error, we recommend checking for the error condition explicitly: