104 if (!isset($file[
'error']) || \is_array($file[
'error'])){
107 switch ($file[
'error']) {
110 case \UPLOAD_ERR_NO_FILE:
112 case \UPLOAD_ERR_INI_SIZE:
113 case \UPLOAD_ERR_FORM_SIZE:
114 throw new FileUploadException($this->getMessageType(self::FILE_SIZE_ERR,$this->getDisplayedFileName($file)));
116 throw new FileUploadException($this->getMessageType(self::UNKNOWN_ERR,$this->getDisplayedFileName($file)));
125 $allowedMimeTypes=$this->getAllowedMimeTypes();
126 if(\is_array($allowedMimeTypes)) {
127 $finfo = new \finfo(\FILEINFO_MIME_TYPE);
128 if(\array_search($finfo->file($file[
'tmp_name']), $this->getAllowedMimeTypes(),
true) ===
false){
129 $this->messages[
'errors'][]=$this->getMessageType(self::MIME_TYPE_ERR, $finfo->file($file[
'tmp_name']),$this->getDisplayedFileName($file));
170 public function upload(
string $destDir=
null,
bool $force=
true,?callable $filenameCallback=
null): void {
171 $destDir??=$this->destDir;
173 $dest=\ROOT.\DS.$destDir.\DS;
174 UFileSystem::safeMkdir(\ROOT.\DS.$destDir.\DS);
176 foreach($_FILES as $file){
177 $this->checkErrors($file);
178 if($this->checkTypeMime($file) && $this->checkFileSize($file)) {
179 $filename = \basename($file[
'name']);
180 if(isset($filenameCallback) && \is_callable($filenameCallback)){
181 $filename=$filenameCallback($filename)??$filename;
183 $dFileName=\htmlspecialchars($filename);
184 if ($force || !\file_exists($dest . $filename)) {
185 if (\move_uploaded_file($file[
'tmp_name'], $dest . $filename)) {
186 $this->messages[
'success'][] = $this->getMessageType(self::SUCCESS_MESSAGE, $dFileName);
188 $this->messages[
'errors'][] = $this->getMessageType(self::UPLOAD_ERR, $dFileName);
191 $this->messages[
'errors'][] = $this->getMessageType(self::EXISTING_FILE_ERR, $dFileName);
195 }
catch(\Exception $e){
196 $this->messages[
'errors'][]=$e->getMessage();