Setting Maximum Directory Size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ruthsimon
Forum Newbie
Posts: 19
Joined: Mon Aug 19, 2002 5:44 pm

Setting Maximum Directory Size

Post by ruthsimon »

Is there a function or way to set the maximum size of a subdirectory?

Users are allowed to upload images to a specific subdirectory, but I want to alert when the subdirectory hits a certain size.

Thanks, Ruth
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Not too sure if that's possible or not, sounds more like a server-setup than a PHP issue.

You could use PHP to add together the sizes of all the files in a directory. If the total size of the files is bigger than X then you can do something from there.

Here's an example..

Code: Select all

<?

$theDir = "myfolder";
$maxSize = "5000"; // size is in bytes.

$d = dir($theDir);
$size = 0;

while(false !== ($file = $d->read()))
{
    if($file != "." && $file != "..") $size = $size + filesize($theDir."/".$file);
    clearstatcache();
}
$d->close();

if($size > $maxSize)
{
    // The directory is full so do something.
}
else
{
    // The directory is no full so do something else.
}

?>
Post Reply