file_validate_size

Definition

file_validate_size($file, $file_limit = 0, $user_limit = 0)
includes/file.inc, line 648

Description

Check that the file's size is below certain limits. This check is not enforced for the user #1.

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.

Related topics

Namesort iconDescription
File interfaceCommon file handling functions.

Code

<?php
function file_validate_size($file, $file_limit = 0, $user_limit = 0) {
  global $user;

  $errors = array();

  // Bypass validation for uid  = 1.
  if ($user->uid != 1) {
    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)));
    }

    $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;
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.