Improved handling errors while uploading (#1246)

This commit is contained in:
Roman Kelesidis 2023-12-21 13:16:41 +07:00 committed by GitHub
commit 486e5cc7d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 28 deletions

View file

@ -19,6 +19,14 @@ class Attach
public $attach_filename = '';
public $filename = '';
public $type = '';
/**
* Upload status code
*
* @var int
*/
public int $error = UPLOAD_ERR_OK;
public $extension = '';
public $file_comment = '';
public $num_attachments = 0; // number of attachments in message
@ -733,30 +741,14 @@ class Attach
$r_file = trim(basename($this->filename));
$file = $_FILES['fileupload']['tmp_name'];
$this->type = $_FILES['fileupload']['type'];
$this->error = $_FILES['fileupload']['error'];
if (isset($_FILES['fileupload']['error'])) {
switch ($_FILES['fileupload']['error']) {
case UPLOAD_ERR_NO_FILE:
bb_die('No file content sent');
break;
case UPLOAD_ERR_INI_SIZE:
bb_die('php.ini<br><b>upload_max_filesize</b> setting: ' . ini_get('upload_max_filesize'));
break;
case UPLOAD_ERR_FORM_SIZE:
bb_die('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
break;
case UPLOAD_ERR_CANT_WRITE:
bb_die('Failed to write file to disk, check permissions');
break;
case UPLOAD_ERR_PARTIAL:
bb_die('The uploaded file was only partially uploaded');
break;
case UPLOAD_ERR_EXTENSION:
bb_die('File upload stopped by extension');
break;
case UPLOAD_ERR_NO_TMP_DIR:
bb_die('Missing a temporary folder');
break;
// Handling errors while uploading
if (isset($this->error) && ($this->error !== UPLOAD_ERR_OK)) {
if (isset($lang['UPLOAD_ERRORS'][$this->error])) {
bb_die($lang['UPLOAD_ERROR_COMMON'] . '<br><br>' . $lang['UPLOAD_ERRORS'][$this->error]);
} else {
bb_die($lang['UPLOAD_ERROR_COMMON']);
}
}

View file

@ -74,6 +74,13 @@ class Upload
*/
public array $errors = [];
/**
* Upload status code
*
* @var int
*/
public int $error = UPLOAD_ERR_OK;
/**
* Image types array
*
@ -102,6 +109,7 @@ class Upload
$this->cfg = array_merge($this->cfg, $cfg);
$this->file = $post_params;
$this->error = $this->file['error'];
// Check upload allowed
if (!$this->cfg['up_allowed']) {
@ -109,11 +117,13 @@ class Upload
return false;
}
// upload errors from $_FILES
if ($this->file['error']) {
$msg = $lang['UPLOAD_ERROR_COMMON'];
$msg .= ($err_desc =& $lang['UPLOAD_ERRORS'][$this->file['error']]) ? " ($err_desc)" : '';
$this->errors[] = $msg;
// Handling errors while uploading
if (isset($this->error) && ($this->error !== UPLOAD_ERR_OK)) {
if (isset($lang['UPLOAD_ERRORS'][$this->error])) {
$this->errors[] = $lang['UPLOAD_ERROR_COMMON'] . '<br><br>' . $lang['UPLOAD_ERRORS'][$this->error];
} else {
$this->errors[] = $lang['UPLOAD_ERROR_COMMON'];
}
return false;
}