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 $attach_filename = '';
public $filename = ''; public $filename = '';
public $type = ''; public $type = '';
/**
* Upload status code
*
* @var int
*/
public int $error = UPLOAD_ERR_OK;
public $extension = ''; public $extension = '';
public $file_comment = ''; public $file_comment = '';
public $num_attachments = 0; // number of attachments in message public $num_attachments = 0; // number of attachments in message
@ -733,30 +741,14 @@ class Attach
$r_file = trim(basename($this->filename)); $r_file = trim(basename($this->filename));
$file = $_FILES['fileupload']['tmp_name']; $file = $_FILES['fileupload']['tmp_name'];
$this->type = $_FILES['fileupload']['type']; $this->type = $_FILES['fileupload']['type'];
$this->error = $_FILES['fileupload']['error'];
if (isset($_FILES['fileupload']['error'])) { // Handling errors while uploading
switch ($_FILES['fileupload']['error']) { if (isset($this->error) && ($this->error !== UPLOAD_ERR_OK)) {
case UPLOAD_ERR_NO_FILE: if (isset($lang['UPLOAD_ERRORS'][$this->error])) {
bb_die('No file content sent'); bb_die($lang['UPLOAD_ERROR_COMMON'] . '<br><br>' . $lang['UPLOAD_ERRORS'][$this->error]);
break; } else {
case UPLOAD_ERR_INI_SIZE: bb_die($lang['UPLOAD_ERROR_COMMON']);
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;
} }
} }

View file

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