function file_validate_size in Drupal 10
Same name and namespace in other branches
- 8 core/modules/file/file.module \file_validate_size()
- 6 includes/file.inc \file_validate_size()
- 7 includes/file.inc \file_validate_size()
- 9 core/modules/file/file.module \file_validate_size()
Checks that the file's size is below certain limits.
Parameters
\Drupal\file\FileInterface $file: A file entity.
int $file_limit: (optional) The maximum file size in bytes. Zero (the default) indicates that no limit should be enforced.
int $user_limit: (optional) The maximum number of bytes the user is allowed. Zero (the default) indicates that no limit should be enforced.
Return value
array An empty array if the file size is below limits or an array containing an error message if it's not.
See also
1 call to file_validate_size()
- ValidatorTest::testFileValidateSize in core/
modules/ file/ tests/ src/ Kernel/ ValidatorTest.php - Tests file_validate_size().
6 string references to 'file_validate_size'
- EditorImageDialog::buildForm in core/
modules/ editor/ src/ Form/ EditorImageDialog.php - FileItem::getUploadValidators in core/
modules/ file/ src/ Plugin/ Field/ FieldType/ FileItem.php - Retrieves the upload validators for a file field.
- FileUploadResource::getUploadValidators in core/
modules/ file/ src/ Plugin/ rest/ resource/ FileUploadResource.php - Retrieves the upload validators for a field definition.
- ImportForm::buildForm in core/
modules/ locale/ src/ Form/ ImportForm.php - Form constructor for the translation import screen.
- template_preprocess_file_upload_help in core/
modules/ file/ file.field.inc - Prepares variables for file upload help text templates.
File
- core/
modules/ file/ file.module, line 194 - Defines a "managed_file" Form API field and a "file" field for Field module.
Code
function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit = 0) {
$user = \Drupal::currentUser();
$errors = [];
if ($file_limit && $file
->getSize() > $file_limit) {
$errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', [
'%filesize' => format_size($file
->getSize()),
'%maxsize' => format_size($file_limit),
]);
}
// Save a query by only calling spaceUsed() when a limit is provided.
if ($user_limit && \Drupal::entityTypeManager()
->getStorage('file')
->spaceUsed($user
->id()) + $file
->getSize() > $user_limit) {
$errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', [
'%filesize' => format_size($file
->getSize()),
'%quota' => format_size($user_limit),
]);
}
return $errors;
}