Skip to content

Commit da2292a

Browse files
authored
file error handling
1 parent bd9292e commit da2292a

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/FilesUploadAndImageResize.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ class FilesUploadAndImageResize
1313
protected int $s = 0;
1414
protected string $format = 'array';
1515
protected array $param = [];
16+
protected array $phpFileUploadErrors = [
17+
0 => 'There is no error, the file uploaded with success',
18+
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
19+
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
20+
3 => 'The uploaded file was only partially uploaded',
21+
4 => 'No file was uploaded',
22+
6 => 'Missing a temporary folder',
23+
7 => 'Failed to write file to disk.',
24+
8 => 'A PHP extension stopped the file upload.',
25+
];
1626
public $uploadedData = '';
1727

1828
/**
@@ -174,21 +184,26 @@ public function uploadFiles(string $fileParamName, int $minImgWidth = 400, array
174184
if (in_array(strtolower($fileInfo), array_map('strtolower', $this->allowExtension)) || empty($this->allowExtension)) {
175185
// Upload and compress only images
176186
if (strtolower($fileInfo) == 'gif' || strtolower($fileInfo) == 'jpeg' || strtolower($fileInfo) == 'jpg' || strtolower($fileInfo) == 'png') {
177-
if ($this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $filePath, $minImgWidth, $waterMark, $quality, $newWidth)) {
178-
if (isset($thumbWidth) && !empty($thumbWidth)) {
179-
foreach ($thumbWidth as $tw) {
180-
$thumbPath = trim($srcThumbPath . $tw . '-' . $fileName);
181-
$this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMark, $quality, $tw);
182-
$this->param['uploaded_thumb_files'][$tw][] = $tw . '-' . $fileName; //All uploaded thumbnail files name are move in this array
183-
$this->param['path_uploaded_thumb_files'][] = trim($srcThumbPath); //All uploaded thumbnail files with complete path
187+
if ($_FILES[$fileParamName]['tmp_name']['error'] > 0) {
188+
$this->param['not_uploaded_files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
189+
$this->param['not_uploaded_files_error'][] = $this->phpFileUploadErrors[$_FILES[$fileParamName]['tmp_name']['error']]; //All not move files name into the destination folder with error [ Note: Check Folder Permission ]
190+
} else {
191+
if ($this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $filePath, $minImgWidth, $waterMark, $quality, $newWidth)) {
192+
if (isset($thumbWidth) && !empty($thumbWidth)) {
193+
foreach ($thumbWidth as $tw) {
194+
$thumbPath = trim($srcThumbPath . $tw . '-' . $fileName);
195+
$this->compressImage($_FILES[$fileParamName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMark, $quality, $tw);
196+
$this->param['uploaded_thumb_files'][$tw][] = $tw . '-' . $fileName; //All uploaded thumbnail files name are move in this array
197+
$this->param['path_uploaded_thumb_files'][] = trim($srcThumbPath); //All uploaded thumbnail files with complete path
198+
}
184199
}
185-
}
186200

187-
$this->param['real_uploaded_files'][] = $val; //All uploaded files with real name
188-
$this->param['uploaded_files'][] = $fileName; //All uploaded files name are move in this array
189-
$this->param['path_uploaded_files'][] = $srcPath; //All uploaded files name are move in this array
190-
} else {
191-
$this->param['not_uploaded_files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
201+
$this->param['real_uploaded_files'][] = $val; //All uploaded files with real name
202+
$this->param['uploaded_files'][] = $fileName; //All uploaded files name are move in this array
203+
$this->param['path_uploaded_files'][] = $srcPath; //All uploaded files name are move in this array
204+
} else {
205+
$this->param['not_uploaded_files'][] = $fileName; //All not move files name into the destination folder [ Note: Check Folder Permission ]
206+
}
192207
}
193208
} else {
194209
// Upload all other files
@@ -218,4 +233,4 @@ public function uploadFiles(string $fileParamName, int $minImgWidth = 400, array
218233
return $e->getMessage();
219234
}
220235
}
221-
}
236+
}

0 commit comments

Comments
 (0)