Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active July 25, 2023 12:50
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save eusonlito/5099936 to your computer and use it in GitHub Desktop.
Save eusonlito/5099936 to your computer and use it in GitHub Desktop.
PHP function to get the folder size including subfolders
<?php
function folderSize ($dir)
{
$size = 0;
foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : folderSize($each);
}
return $size;
}
@carpad88
Copy link

carpad88 commented Aug 1, 2020

This function converts bytes:

function sizeFilter($bytes)
    {
        $label = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');

        for ($i = 0; $bytes >= 1024 && $i < (count($label) - 1); $bytes /= 1024, $i++) ;

        return (round($bytes, 2) . " " . $label[$i]);
    }

@kangjeki
Copy link

Perfect..

@Chrishow2
Copy link

Great!

Copy link

ghost commented May 20, 2021

Va perfecto, gracias 👍

@ahtun
Copy link

ahtun commented May 22, 2023

-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)

    $f = './path/directory';
    $io = popen ( '/usr/bin/du -sh ' . $f, 'r' );
    $size = fgets ( $io, 4096);
    $size = substr ( $size, 0, strpos ( $size, "\t" ) );
    pclose ( $io );
    echo 'Directory: ' . $f . ' => Size: ' . $size;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment