Directory Size
By: Daniel

Creating a script to display the size of a directory is fairly simple. First off you need to know the path to the directory. After that is known you can use the following fuction to display the filesize:

<?php
function dirsize($dir) {
  $dirs = array($dir);
    while(!empty($dirs)) { // Loop through all directories
      $path = array_shift($dirs);

      foreach(glob($path.'/*') AS $next) {
        if(is_dir($next)) {
          $dirs[] = $next; // Add another directory to the loop
        } else {
          $size += filesize ($next); // Add filesize to count
        }
      }
    }
  return $size; // Return the total filesize of the directory
}
?>

This function returns a string with the size of the directory in bytes. You can however use the Convert byte count function to produce a readable format.

An example of this code could be:

<?php
$path = '.';
echo dirsize($path); // would produce something like 732952 or 715.79 KB using the Convert byte count function
?>
 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...
del.icio.us:Directory Size digg:Directory Size spurl:Directory Size wists:Directory Size simpy:Directory Size newsvine:Directory Size blinklist:Directory Size furl:Directory Size reddit:Directory Size fark:Directory Size blogmarks:Directory Size Y!:Directory Size smarking:Directory Size magnolia:Directory Size segnalo:Directory Size

2 Responses to “Directory Size”

  1. Sport Talk Says:

    Do I have to be a member to post to this blog?

  2. Dik Says:

    Thanks alot webmaster I like your site

Leave a Reply

eXTReMe Tracker
geovisitors