Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active September 24, 2024 18:47
Show Gist options
  • 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;
}
@mfka
Copy link

mfka commented Oct 25, 2016

well Done! And thank you :)

@Blueblazer172
Copy link

awasome function 👍 💯

@mdimitris
Copy link

The result is in bytes or KB? Thank you!

@vishnu1991
Copy link

@mdimitris " bytes"
Hope you must have got the answer 😄

@comm1x
Copy link

comm1x commented Mar 21, 2017

Thanks

@miladghiravani
Copy link

miladghiravani commented Apr 4, 2017

Thanks

@anhphamt
Copy link

many thanks.

@redoxate
Copy link

<3

@sakonachhoeurng
Copy link

hi what is folderSize($each); I think this one should be undefined

@ArsSirek
Copy link

ArsSirek commented Nov 9, 2017

@sakonachhoeurng Hi, you probably noticed already) but just in case, It's the function name - recursive call, to count all subfolders as fell.

@hellerbenjamin
Copy link

Perfect.

@chrisw90uk
Copy link

Perfect - thanks for your help!

@1uc
Copy link

1uc commented Jan 30, 2018

Nice, however, at least on Linux, it won't include dot-files (since * does not match .some-file). Furthermore, on my machine an empty directory still occupies some space (4096 bytes according to du), this is also not included in folderSize.

Oh, and thanks, I'm using something very similar.

@brehehe
Copy link

brehehe commented Mar 5, 2018

Cool thank you

@CrashedBboy
Copy link

Thanks! It helps me a lot.

@ipara-dev
Copy link

ipara-dev commented Jul 19, 2018

For directory size:
if($size<1024){$size=$size." Bytes";}
elseif(($size<1048576)&&($size>1023)){$size=round($size/1024, 1)." KB";}
elseif(($size<1073741824)&&($size>1048575)){$size=round($size/1048576, 1)." MB";}
else{$size=round($size/1073741824, 1)." GB";}
return $size;

@faustfizz
Copy link

Thanks a lot

@DeBelserArne
Copy link

Great, thanks! Using this!

@buttflattery
Copy link

buttflattery commented Sep 10, 2019

Nice but nothing beats the Swag of the following when the directory size is large.

    $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;

@hemaguse
Copy link

Gracias!!

@2gbeh
Copy link

2gbeh commented May 12, 2020

@eusonlito Perfect 👍

@SparcV8
Copy link

SparcV8 commented Jul 25, 2020

Regards,
some fast solution for Windows?

@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;

@webdev23
Copy link

webdev23 commented Jun 2, 2024

In GigaBytes:

return $size / 1e-9; //GigaBytes

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