Jump to content

How Much Space A Folder Is Taken Up


private_guy

Recommended Posts

Hi there all,

 

How is everyone? Well I was just wondering if somebody got teach me how to make a PHP Script which tells you how much space a folder is taken up?

 

Example: I have a folder called ./files, I want a script which will tell me howmuch space its taken up :).

 

Thanks.

 

Best Regards,

Chill_Guy

Link to comment
Share on other sites

disk_total_space does not calulate a size a of a directory. It only calculates the size of the hard drive or a partition within a drive.

 

private_guy I wrote this function a while ago:

<?php

function folder_size($path, $folder=null)
{
    if(!isset($dir_size))
    {
        $dir_size = 0;
    }

    $folder_path = $path . $folder . '/';

    $handle = opendir($folder_path);

    $ignore = array('.', '..');

    while ( false !== ($file = readdir($handle)))
    {
        if (!in_array($file, $ignore))
        {
            if(!is_dir($folder_path . '/' . $file))
            {
                $dir_size += filesize($folder_path . '/' . $file);
            }
            elseif(is_dir($folder_path . '/' . $file))
            {
                $dir_size += folder_size($folder_path, $file);
            }
        }
    }

    closedir($handle);

    return $dir_size;
}

$folder_size = folder_size('./file');

echo 'Total folder size = ' . number_format(($folder_size/1024/1024), 2) . ' MB';

?>

This will calculate the file size of a given directory.

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.