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
?>
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

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

geovisitors