Posted to PHP.NET on 4/9/2006

My first contribution to PHP.NET, but alas, its only an enhancement (prior post tweak).

This is a little PHP utility that will list all the subdirectories and their sizes from a given starting directory (e.g., the root folder).

I didn't just want the grand total of the directories and subdirectories scanned, as a previous poster graciously listed, but the total for each directory. I can then take that list into my spreadsheet app, and sort it based on size and find out which directories are eating up the most (or those that are empty).

I put the tilde (~) in there for easier porting into your spreadsheet app, using it as a delimiter.

In addition, I store this utility in a protected directory one level down from my root, so using the "../" does my entire root.

<?
echo "<br><br>".dskspace("../");
function dskspace($dir)
{	$s = stat($dir);
	$space = $s["blocks"]*512;
	if (is_dir($dir))
	{	$dh = opendir($dir);
		while (($file = readdir($dh)) !== false)
		if ($file != "." and $file != "..")
			$space += dskspace($dir."/".$file);
		echo "<br>".round(($space/1024)/1024,2)." Mb ~ <strong>".$dir.$file."</strong>";
		closedir($dh);
	}
	return $space;
}
?>

Never thought I'd know enough to post on PHP.NET :)