Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better exceptions for file upload #2902

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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