enhance upload error messages
This commit is contained in:
parent
6f35599523
commit
fde044cc1a
@ -221,7 +221,7 @@ class UploadedFile extends File
|
||||
return $target;
|
||||
}
|
||||
|
||||
throw new FileException(sprintf('The file "%s" is not valid', $this->getPathname()));
|
||||
throw new FileException($this->getErrorMessage($this->getError()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -246,4 +246,36 @@ class UploadedFile extends File
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an informative upload error message
|
||||
*
|
||||
* @param int $code The error code returned by upload attempt
|
||||
*
|
||||
* @return string The error message regarding specified error code
|
||||
*/
|
||||
private function getErrorMessage($errorCode)
|
||||
{
|
||||
static $errors = array(
|
||||
UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d kb)',
|
||||
UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form',
|
||||
UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded',
|
||||
UPLOAD_ERR_NO_FILE => 'No file was uploaded',
|
||||
UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk',
|
||||
UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory',
|
||||
UPLOAD_ERR_EXTENSION => 'File upload was stopped by a php extension',
|
||||
);
|
||||
|
||||
$maxFilesize = 0;
|
||||
if ($errorCode === UPLOAD_ERR_INI_SIZE) {
|
||||
$maxFilesize = self::getMaxFilesize() / 1024;
|
||||
}
|
||||
|
||||
$message = isset($errors[$errorCode]) ? $errors[$errorCode] : null;
|
||||
if (!isset($message)) {
|
||||
$message = 'The file "%s" was not uploaded due to an unknown error';
|
||||
}
|
||||
|
||||
return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user