Same name and namespace in other branches
  1. 10 core/modules/file/file.module \file_validate_size()
  2. 6.x includes/file.inc \file_validate_size()
  3. 8.9.x core/modules/file/file.module \file_validate_size()
  4. 9 core/modules/file/file.module \file_validate_size()

Checks that the file's size is below certain limits.

Parameters

$file: A Drupal file object.

$file_limit: An integer specifying the maximum file size in bytes. Zero indicates that no limit should be enforced.

$user_limit: An integer specifying the maximum number of bytes the user is allowed. Zero indicates that no limit should be enforced.

Return value

An array. If the file size exceeds limits, it will contain an error message.

See also

hook_file_validate()

Related topics

1 call to file_validate_size()
FileValidatorTest::testFileValidateSize in modules/simpletest/tests/file.test
Test file_validate_size().
3 string references to 'file_validate_size'
file_field_widget_upload_validators in modules/file/file.field.inc
Retrieves the upload validators for a file field.
theme_file_upload_help in modules/file/file.field.inc
Returns HTML for help text based on file upload validators.
user_validate_picture in modules/user/user.module
Validates an image uploaded by a user.

File

includes/file.inc, line 1833
API for handling file uploads and server file management.

Code

function file_validate_size(stdClass $file, $file_limit = 0, $user_limit = 0) {
  global $user;
  $errors = array();
  if ($file_limit && $file->filesize > $file_limit) {
    $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array(
      '%filesize' => format_size($file->filesize),
      '%maxsize' => format_size($file_limit),
    ));
  }

  // Save a query by only calling file_space_used() when a limit is provided.
  if ($user_limit && file_space_used($user->uid) + $file->filesize > $user_limit) {
    $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array(
      '%filesize' => format_size($file->filesize),
      '%quota' => format_size($user_limit),
    ));
  }
  return $errors;
}