Finding the size of Directories
15
A small recursive php function to determine the size of a directory by adding all it's contents together and returning them as an integer.
<?php
/****************************************************************************
* Usage is simple:
* $foo = get_size('/directory/another/whatsize/');
* also can be used like $bar = get_size('/directory/filesize.jpg');
* if you really wanted to.
*****************************************************************************/
function get_size($path)
{
if(!is_dir($path))
{
return filesize($path);
}
$dir = opendir($path);
while($file = readdir($dir))
{
if(is_file($path."/".$file))
{
$size+=filesize($path."/".$file);
}
if(is_dir($path."/".$file) && $file!="." && $file !="..")
{
$size +=get_size($path."/".$file);
}
}
return $size;
}
?>






I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?
Both have something special though:
- this one works for both dirs and files, but it throws an error if $path is neither dir or file (just malformed)
- the other checks for correct dir name, but doesn't accept files at all