You are here

function filefield_validate_size in FileField 6.3

An #upload_validators callback. Check the file size does not exceed a limit.

Parameters

$file: A Drupal file object.

$file_limit: An integer value limiting the maximum file size in bytes.

$file_limit: An integer value limiting the maximum size in bytes a user can upload on the entire site.

Return value

An array of any errors cause by this file if it failed validation.

File

./filefield.module, line 802
FileField: Defines a CCK file field type.

Code

function filefield_validate_size($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),
    ));
  }

  // Bypass user limits for uid  = 1.
  if ($user->uid != 1) {
    $total_size = file_space_used($user->uid) + $file->filesize;
    if ($user_limit && $total_size > $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;
}