Jump to content

[SOLVED] PHP Script to Limit Quota


hoopplaya4

Recommended Posts

Hi all,

 

Let me do my best to explain the situation.  I'm currently on a hosting plan and have a certain folder that users upload files onto through a web form.  (For example, /home/users/public_html/uploads)

 

I want to put a "cap" on the amount of disk space on that specific folder (uploads).  I've spoken with my provider, and they cannot do it for me.

 

So, my question is, how would I go about creating a script that, right before a user uploads, calculates the current space usage on the folder, compares it to my allowed usage (say 500 MB), and then either allows or disallows the upload based on their quota.

 

Any point in the right direction would be fantastic!  Thanks.

Link to comment
Share on other sites

Without reading, these are the fastest methods...

 

Linux...

 

<?php

$f = './path/directory';

$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );

$size = fgets ( $io, 4096);

$size = substr ( $size, 0, strpos ( $size, '\t' ) );

pclose ( $io );

echo 'Directory: ' . $f . ' => Size: ' . $size;

?>

 

Windows...

 

<?php

$f = 'f:/www/docs';

$obj = new COM ( 'scripting.filesystemobject' );

if ( is_object ( $obj ) )
{
	$ref = $obj->getfolder ( $f );

	echo 'Directory: ' . $f . ' => Size: ' . $ref->size;

	$obj = null;
}
else
{
	echo 'can not create object';
}

 

 

Or if you don't have access to system calls you can use this...

 

This gives the size based on the BLOCK SIZE, which is the actually disk space used.

 

<?

function dirsize ( $dir, $block = 4096 )
{
    $dirs = array ( $dir );

    $size = array ( 0, 0 );

    while ( isset ( $dirs[0] ) )
    {
        $path = array_shift ( $dirs );

        foreach ( glob ( $path . '/*' ) AS $next )
        {
            if ( is_dir ( $next ) )
            {
                $dirs[] = $next;
            }
            else
            {
                $fs = filesize ( $next );

                $size[0] += $fs;

                if ( $fs != 0 && $fs <= $block )
                {
                    $size[1] += $block;
                }
                else if ( $fs > $block )
                {
                    $ts = ceil ( ( $fs / $block ) );

                    $size[1] += ( $block * $ts );
                }
            }
        }
    }

    return $size;
}

/* no trailing '/' */

$path = '.';

// array[0] = total file size, array[1] = total disk space used, (for the directory)

print_r ( dirsize ( $path ) );

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.