How to get the size of a directory in PHP



Sometimes we need to know how much is the size of a directory including files inside it in PHP.

Here is a nice simple PHP function to do so.

function getDirectorySize($dir){
	$size = 0;

	foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
		$size += is_file($each) ? filesize($each) : folderSize($each);
	}

	return $size;
}

Just call the function and it takes one parameter which is directory location.

For example we can use it like:

getDirectorySize("some/directory/");
loading...

Leave a Reply

Your email address will not be published. Required fields are marked *