I need a mechanism for managing users' storage space. I have multiple content types with file upload fields. Admin should be able to set it globally, or per individual user.

My initial approach is to add a field in the admin configuration menu, for admin to set the default limit, and a field for users for individual space allocations.

When a user goes to create new content, I check their used space to see if they are maxed out. Then when they are about to upload a file, I'll check the file size and see if it will put the user over the limit. If not, I let the upload occur.

The Drupal API gives us a file_space_used() function, which gives the total file space used by a particular user, so that's pretty cool.

Am I on the right track?

EDIT AFTER ANSWER: syd asked me to post my script, and I forgot.

function mymod_file_validate($file)
    global $user;
    $ulimit = 0;    // user limit

    // get the limits per role (I had two to check against, and vals are
    // in megabytes (hence the *1000000))
    if (array_search(HL_ROLE_REG, $user->roles))
        $ulimit = variable_get(HL_VARKEY_REGSPACE) * 1000000;

    if (array_search(HL_ROLE_PREM, $user->roles))
        $ulimit = variable_get(HL_VARKEY_PREMSPACE) * 1000000;

    return (file_validate_size($file, 0, $ulimit));

Drupal does all the work.

I made a call to file_validate_size() in my hook_file_validate() handler. I also made an admin form to modify each role's allowable space (using variable_get/set), and it works great.

Next step is to create a table containing non-default values (keyed by uid), and an admin tool to set individual users' allowable space. I'll then update my hook_file_validate() handler to check for the user in that table. If not, then check the default.

  • Hi Vintorg, may you post the script please? – Tyler Durden Jul 26 '12 at 11:25
  • I will. Can you tell me how to format my script in this comment window? – vintorg Jul 26 '12 at 21:05
  • Edit your answer or your question. It's easier. – Tyler Durden Jul 27 '12 at 8:11
  • 1
    I added my code at the top. Sorry for the late response, but I forgot. – vintorg Sep 24 '12 at 4:38

Probably you should use file_validate_size()

function file_validate_size(stdClass $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)));

    // 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;

Checks 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.

  • Yeah, that looks like the way to do it. What I'm thinking about is how to store a field for the user entity. I am trying to avoid creating a new table if possible and utilize what Drupal offers me. – vintorg Jul 7 '12 at 0:17
  • I'm thinking I will set the default value in the DB using variable_set(). On the user side, I want to hide it from the users, so it looks like I'll create a table holding any non-default values. – vintorg Jul 7 '12 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.