Calculate directory size & display %?

Discussion in 'PHP' started by D12Eminem989, Aug 14, 2011.

  1. D12Eminem989

    D12Eminem989 New Member

    Joined:
    Aug 14, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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:

    Total Size: 434 MB
    Total Folders: 32
    Total Files: 12345
    Total % Used: 35%

    So in the index.php I would define a total folder size in megabytes of how much they can use and it would calculate what % they have left. If that makes sense.

    Thanks in advance!
     
  2. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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:
    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:
    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:
    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;
        }
    }
     
  3. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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:
    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 counters
                
    if($f != "." && $f != "..")
                {
                    
    // make sure the value of $f does not equal . or .., since it doesnt we check if each value is a file or folder
                    
    if(is_dir($p $f))
                    {
                        
    $dC++;
                    }
                    else if(
    is_file($p $f))
                    {
                        
    $fC++;
                    }
                    
                }
            }
            
            
    $l = array($dC,$fC);
            return 
    $l;
        }
        else
        {
            
    // person added delimter for path lets get to work without adding the delimter in code
            
    $h opendir($p $s);
            while((
    $f readdir($h)) !== false)
            {
                
    //loop through the directory and create counters
                
    if($f != "." && $f != "..")
                {
                    
    // make sure the value of $f does not equal . or .., since it doesnt we check if each value is a file or folder
                    
    if(is_dir($p $s $f))
                    {
                        
    $dC++;
                    }
                    else if(
    is_file($p $s $f))
                    {
                        
    $fC++;
                    }
                    
                }
            }
            
            
    $l = array($dC,$fC);
            return 
    $l;
        }
    }
     
    Last edited: Aug 16, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice