Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP – get the size of a directory

What is the best way to get the size of a directory in PHP? I'm looking for a lightweight way to do this since the directories I'll use this for are pretty huge.

There already was a question about this on SO, but it's three years old and the solutions are outdated.(Nowadays fopen is disabled for security reasons.)

like image 774
js-coder Avatar asked Oct 16 '25 13:10

js-coder


1 Answers

Is the RecursiveDirectoryIterator available to you?

$bytes = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $i) 
{
  $bytes += $i->getSize();
}
like image 161
Rob Agar Avatar answered Oct 19 '25 05:10

Rob Agar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!