Get Directory size Recursively in PHP

Hi everyone…

Following is the function to get the size of directory whose path is passed to this function. This function recursively finds the size of all the subdirectories and files.

function GetDirSize ($directoryPath)
{
$delimeter = DIRECTORY_SEPARATOR;
$size = 0;
if (is_dir($directoryPath))
{
if ($dh = opendir($directoryPath))
{
while (($file = readdir($dh)) != false)
{
if($file != “.” && $file != “..”)
{
if(is_dir($directoryPath .$delimeter. $file))
{
$size += GetDirSize ($directoryPath.$delimeter.$file);
}
else
{
$size += filesize($directoryPath.$delimeter.$file);
}
}
}
}
}
else
$size += filesize($directoryPath);
@closedir($dh);
return $size;
}
Keep Smiling 🙂
Advertisements