Calculate directory size & display %?

I am looking for a simple PHP script that will calculate the directory folder defined and then all sub folders under it.

I found one, however it doesn't display how I want. If it's possible to in the index.php define a folder size that will show the end user how much space they are using and then a % part to let them know how much of it there using. So basically it shows this as an example:

You can use these functions I created to get the file count and folder count of a directory. I have not made one that gets the size of a directory but you would need to check every sub directory for file and if they exist get their size until every sub folder and all files have their size added together. Maybe my functions will help you out with your own implementation to solve this problem. Of course one function could get the file count, folder count, and total size. If I do solve this problem I'll post the solution as a single function that handles everything because it would be memory efficient to do the counting while I iterate once instead of calling multiple functions and running multiple loops. Hope this helps inspire you to finish what I started.

Folder Count

PHP Code:

function folderCount($p)
{
$fN = 0;// holds the number of folders in a folder
$pL = strlen($p);
$pE = substr($p,$pL - 1,$pL);
if(!is_dir($p))
{
// path is not a valid folder
return 0;
}
else
{
if($pE == "/")
{
//folder is valid lets open it up and take a peek
$f = scandir($p);// read folders content and save it as an array

Instead of using those two functions just use this one function. It does not open all sub directories and get their folder and file counts. The other two where in accurate so I needed to use a better version that works. This has been tested to work and is accurate in both file and directory count. I'll try to fix it so it shows the whole file count including files in sub-directories and sub-directories. While the loop is at it I'll try to get it to show the total file sizes as well.

PHP Code:

function fileCount($p,$s = "/"){$pL = strlen($p); // length of the path string$pE = substr($p,$pL - 1, $pL);// holds if the end value is / or not.$dC = 0; // used to hold the count of folders$fC = 0; // holds the count of files.$h = '';$f = ''; if($pE == $s) {// person added delimter for path lets get to work without adding the delimter in code$h = opendir($p); while(($f = readdir($h)) !== false) {//loop through the directory and create countersif($f != "." && $f != "..") {// make sure the value of $f does not equal . or .., since it doesnt we check if each value is a file or folderif(is_dir($p . $f)) {$dC++; } else if(is_file($p . $f)) {$fC++; }