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
https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/
Share on other sites

Practically i can think of two ways. The first, clean and simple is disk_total_space(), while the other should be running a recursive function through all files and subfolders of that folder and use filesize(). Anyway the first should do it.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.