From 486e5cc7d260366e2965762b62fd4b9c8ceff0f2 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Thu, 21 Dec 2023 13:16:41 +0700 Subject: [PATCH] Improved handling errors while uploading (#1246) --- src/Legacy/Attach.php | 38 ++++++++++++++---------------------- src/Legacy/Common/Upload.php | 20 ++++++++++++++----- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/Legacy/Attach.php b/src/Legacy/Attach.php index 8b4c275be..c0862d046 100644 --- a/src/Legacy/Attach.php +++ b/src/Legacy/Attach.php @@ -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
upload_max_filesize 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'] . '

' . $lang['UPLOAD_ERRORS'][$this->error]); + } else { + bb_die($lang['UPLOAD_ERROR_COMMON']); } } diff --git a/src/Legacy/Common/Upload.php b/src/Legacy/Common/Upload.php index 589ca247e..8e5129248 100644 --- a/src/Legacy/Common/Upload.php +++ b/src/Legacy/Common/Upload.php @@ -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'] . '

' . $lang['UPLOAD_ERRORS'][$this->error]; + } else { + $this->errors[] = $lang['UPLOAD_ERROR_COMMON']; + } return false; }