Skip to content

Commit

Permalink
Better exceptions for file upload (#2902)
Browse files Browse the repository at this point in the history
* Better exceptions for file upload

* PHPCS fixes

* PHPCS fixes
  • Loading branch information
fballiano authored Jan 6, 2023
1 parent 9faf7cb commit 8020ce0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Varien/File/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@

class Varien_File_Uploader
{
public const UPLOAD_ERRORS = [
UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded',
UPLOAD_ERR_NO_FILE => 'No file was uploaded',
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload'
];

/**
* Uploaded file handle (copy of $_FILES[] element)
*
Expand Down Expand Up @@ -150,6 +160,10 @@ public function __construct($fileId)
{
$this->_setUploadFileId($fileId);
if (empty($this->_file['tmp_name']) || !file_exists($this->_file['tmp_name'])) {
$errorCode = $this->_file['error'] ?? 0;
if ($errorCode && isset(self::UPLOAD_ERRORS[$errorCode])) {
throw new Exception(self::UPLOAD_ERRORS[$errorCode]);
}
$code = empty($this->_file['tmp_name']) ? self::TMP_NAME_EMPTY : 0;
throw new Exception('File was not uploaded.', $code);
} else {
Expand Down

0 comments on commit 8020ce0

Please sign in to comment.