View Single Post
Ambitious contributor
15Aug2011,08:13  
pein87's Avatar
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
        
            
if(!$f)
            {
                
//check if variable has a value, in this case it does not return 0 or false
                
return 0;
            }
            else
            {
                
$fC count($f);
                
$i 0;
                for (
$i 0$i $fC$i++)
                {
                    if(
is_dir($f[$i]) && $f[$i] != "." $f[$i] != "..")
                    {
                        
$fN += 1;
                    }
                }
            
                return 
$fN;
            }
        }
        else
        {
            
//folder is valid lets open it up and take a peek
            
$f scandir($p "/");// read folders content and save it as an array
        
            
if(!$f)
            {
                
//check if variable has a value, in this case it does not return 0 or false
                
return 0;
            }
            else
            {
                
$fC count($f);
                
$i 0;
                for (
$i 0$i $fC$i++)
                {
                    if(
is_dir($f[$i]) && $f[$i] != "." $f[$i] != "..")
                    {
                        
$fN += 1;
                    }
                }
            
                return 
$fN;
            }
        }
        
        
    }

File Count

PHP Code:
function fileCount($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
        
            
if(!$f)
            {
                
//check if variable has a value, in this case it does not return 0 or false
                
return 0;
            }
            else
            {
                
$fC count($f);
                
$i 0;
                for (
$i 0$i $fC$i++)
                {
                    if(
is_file($f[$i]))
                    {
                        
$fN += 1;
                    }
                }
            
                return 
$fN;
            }
        }
        else
        {
            
//folder is valid lets open it up and take a peek
            
$f scandir($p "/");// read folders content and save it as an array
        
            
if(!$f)
            {
                
//check if variable has a value, in this case it does not return 0 or false
                
return 0;
            }
            else
            {
                
$fC count($f);
                
$i 0;
                for (
$i 0$i $fC$i++)
                {
                    if(
is_file($f[$i]))
                    {
                        
$fN += 1;
                    }
                }
            
                return 
$fN;
            }
        }
        
        
    }
    

Percent

PHP Code:
function percent($n,$d)
{
    if(!
is_numeric($n) && !is_numeric($d))
    {
        return 
0;//both aren't a number or one is and the other isn't
    
}
    else
    {
        
$p = ($n $d) * 100;

        return 
$p;
    }