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 ...

8 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

  3. accessoires iphone Says:

    Je voulais juste dire thème aweseome et post.I grande comme la façon dont vous Wright. Avez-vous pensé de l’écriture freelance?

  4. Martin Kody Says:

    News is a great thing.

  5. Serita Lehoullier Says:

    I have read several excellent stuff here. Definitely worth bookmarking for revisiting. I wonder how much attempt you put to create this kind of fantastic informative web site.

  6. runescape classic private server Says:

    I have discovered that clever real estate agents just about everywhere are getting set to FSBO Marketing and advertising. They are acknowledging that it’s not just placing a sign post in the front property. It’s really regarding building relationships with these sellers who at some point will become consumers. So, after you give your time and energy to serving these retailers go it alone – the “Law of Reciprocity” kicks in. Thanks for your blog post.

  7. Anxiety Symptoms Says:

    It’s actually a great and helpful piece of info. I’m happy that you shared this useful information with us. Please stay us up to date like this. Thank you for sharing.

  8. party tops Says:

    How do I start blogging and make money at the same time?

geovisitors