directory_size() function for PHP
Here’s a quick PHP function that returns the sum of all filesizes in a directory. It does this by looping through all files it finds in the directory you pass to it. If a subdirectory is found the function calls itself recursively.
To use: pass the directory you want to check as the only variable, eg: directory_size(’/home/web/directory’)
PHP:
-
function directory_size($dir)
-
/* Return the file size for files in directory
-
Inputs:
-
$dir The directory to check
-
Outputs:
-
The total file size of all files in $dir
-
*/
-
{
-
$retval = 0;
-
{
-
if ($file!=“.” && $file!=“..”)
-
{
-
{
-
$retval = $retval + directory_size($dir.“/”.$file);
-
}
-
else
-
{
-
}
-
}
-
}
-
return $retval;
-
}
One thing to note is that the file size returned is in bytes. If you want to get it in something more friendly you’ll have to do some math.
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.