Finding the size of Directories





15
Date Submitted Sun. Aug. 27th, 2006 10:44 AM
Revision 1 of 1
Helper mceppi
Tags filesize | PHP
Comments 2 comments
A small recursive php function to determine the size of a directory by adding all it's contents together and returning them as an integer.

<?php
/****************************************************************************
 * Usage is simple:
 * $foo = get_size('/directory/another/whatsize/');
 * also can be used like $bar = get_size('/directory/filesize.jpg');
 * if you really wanted to.
 *****************************************************************************/

function get_size($path)
{
        if(!is_dir($path))
        {
                return filesize($path);
        }
        $dir = opendir($path);
        while($file = readdir($dir))
        {
                if(is_file($path."/".$file))
                {
                        $size+=filesize($path."/".$file);
                }
                if(is_dir($path."/".$file) && $file!="." && $file !="..")
                {
                        $size +=get_size($path."/".$file);
                }

        }
        return $size;
}
?>
 

Marco Ceppi

www.marcoceppi.com

Comments

Comments Especially with no exec()
Fri. Sep. 22nd, 2006 7:03 AM    Scripter sehrgut
Comments Kind of dupe
Tue. Oct. 10th, 2006 12:00 AM    Helper jarfil

Voting