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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: