CoderZone.org

Category: >> PHP Code >> Calculate File/Folder Size In A Directory Bookmark and Share

<< lastnext >>

Snippet Name: Calculate File/Folder Size In A Directory

Description: This slick PHP Class calculates information on files, including the size, the number of files & all folders in a given directory.

Also see:
» Get the relative path to the root ...
» Simple image resize on mouseover
» Get database and table sizes
» Simple directory viewer / browser
» Resize and convert an image to a P...
» Read a web page directly in to a s...
» Recursive Directory Browser
» Copy a folder in Visual Basic
» Get File Size Over HTTP
» Saving Remote Images With PHP
» Include All Files In A Directory
» More File Type Detection in PHP
» Get Current Page URL
» Get Current Directory Name
» Measure script run time
» USER - connection information
» USER - User Information 2
» USER Information
» USERS: view specific user info
» Show info on current context
» List security related profile info...
» Write to File example
» Get Path Info
» Nicely Format File Size
» Check if a file exists
» Export table to Excel or MS Word f...
» Get MySQL Database Size
» Show basic image info
» Compute Table Size
» Sized Pop-Up Windows

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

<?php
 
// calculate.directory.class.php
// Credits: BitRepository.com
// URL: http://www.bitrepository.com/web-programming/php/
// calculate-the-size-number-of-files-folders-of-a-directory.html
 
 
class Directory_Calculator {
 
    var $size_in;
     var $decimals;
 
    function calculate_whole_directory($directory)
    {
        if ($handle = opendir($directory))
        {
        $size = 0;
          $folders = 0;
          $files = 0;
 
        while (false !== ($file = readdir($handle)))
          {
            if ($file != "." && $file != "..")
               {
                   if(is_dir($directory.$file))
                   {
                $array = $this->calculate_whole_directory($directory.$file.'/');
                $size += $array['size'];
                    $files += $array['files'];
                    $folders += $array['folders'];
                   }
                   else
                   {
                $size += filesize($directory.$file);
                    $files++;
                   }
            }
         }
         closedir($handle);
         }
 
           $folders++;
 
    return array('size' => $size, 'files' => $files, 'folders' => $folders);
    }
 
     function size_calculator($size_in_bytes)
    {
        if($this->size_in == 'B')
        {
        $size = $size_in_bytes;
        }
        elseif($this->size_in == 'KB')
        {
        $size = (($size_in_bytes / 1024));
        }
        elseif($this->size_in == 'MB')
        {
        $size = (($size_in_bytes / 1024) / 1024);
        }
        elseif($this->size_in == 'GB')
        {
        $size = (($size_in_bytes / 1024) / 1024) / 1024;
        }
 
        $size = round($size, $this->decimals);
 
          return $size;
     }
 
     function size($directory)
     {
     $array = $this->calculate_whole_directory($directory);
     $bytes = $array['size'];
     $size = $this->size_calculator($bytes);
     $files = $array['files'];
     $folders = $array['folders'] - 1; // exclude the main folder
 
     return array('size'    => $size,
                   'files'   => $files,
                   'folders' => $folders);
     }
}
?>
 
Here&#39;s an usage example of this class:
 
example.php
 
<?php
set_time_limit(10000);
 
include 'calculate.directory.class.php';
 
/* Path to Directory - IMPORTANT: with '/' at the end */
 
$directory = '/home/mywebsite.com/public_html/'; 
 
/* Calculate size in: B (Bytes), KB (Kilobytes), MB (Megabytes), GB (Gigabytes) */
 
$size_in = 'MB';
 
/* Number of decimals to show */
 
$decimals = 2;
 
$directory_size = new Directory_Calculator;
 
/* Initialize Class */
 
$directory_size->size_in = $size_in;
$directory_size->decimals = $decimals;
 
// return an array with: size, total files & folders
$array = $directory_size->size($directory); 
 
echo "The directory <em>".$directory."</em> has a size of 
     ".$array['size']." ".$size_in.", ".$array['files']." files & 
     ".$array['folders']." folders.";
?>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7