Calculating a directories size in PHP

Posted 5 April 2004

This function provides a heavily optimised method for calculating the size of large directories.

<?php
/**
 * Calculate the size of a directory by iterating its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.2.0
 * @link        http://aidanlister.com/2004/04/calculating-a-directories-size-in-php/
 * @param       string   $directory    Path to directory
 */
function dirsize($path)
{
    // Init

    $size = 0;
 
    // Trailing slash

    if (substr($path, -1, 1) !== DIRECTORY_SEPARATOR) {
        $path .= DIRECTORY_SEPARATOR;
    }
 
    // Sanity check

    if (is_file($path)) {
        return filesize($path);
    } elseif (!is_dir($path)) {
        return false;
    }
 
    // Iterate queue

    $queue = array($path);
    for ($i = 0, $j = count($queue); $i < $j; ++$i)
    {
        // Open directory

        $parent = $i;
        if (is_dir($queue[$i]) && $dir = @dir($queue[$i])) {
            $subdirs = array();
            while (false !== ($entry = $dir->read())) {
                // Skip pointers

                if ($entry == '.' || $entry == '..') {
                    continue;
                }
 
                // Get list of directories or filesizes

                $path = $queue[$i] . $entry;
                if (is_dir($path)) {
                    $path .= DIRECTORY_SEPARATOR;
                    $subdirs[] = $path;
                } elseif (is_file($path)) {
                    $size += filesize($path);
                }
            }
 
            // Add subdirectories to start of queue

            unset($queue[0]);
            $queue = array_merge($subdirs, $queue);
 
            // Recalculate stack size

            $i = -1;
            $j = count($queue);
 
            // Clean up

            $dir->close();
            unset($dir);
        }
    }
 
    return $size;
}
?>

A quick usage example:

<?php
// Show the current directory

echo dirsize('.');
echo "\n";
 
// We can use size_readable to format the result

// http://aidanlister.com/repos/v/function.size_readable.php

echo size_readable(dirsize('.'));
?>

This would output: 591677 591.68 K