function.dirsize.php
Calculate the size of a directory by iterating its contents
New algorithm means only 1 branch is open in memory at a time so extremely large directories can be calculated quickly.
- Author: Aidan Lister <aidan@php.net>
- Version: 1.2.0
- Link: http://aidanlister.com/repos/v/function.dirsize.php
- Views: 19955
- Downloads: 1767
Source
<?php
/**
* Calculate the size of a directory by iterating its contents
*
* New algorithm means only 1 branch is open in memory at a time
* so extremely large directories can be calculated quickly.
*
* @author Aidan Lister <aidan@php.net>
* @version 1.2.0
* @link http://aidanlister.com/repos/v/function.dirsize.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;
}
?>
Example
<pre>
<?php
require_once 'function.dirsize.php';
// Show the current directory
echo dirsize('.');
echo "\n";
// We can use size_readable to format the result
require_once 'function.size_readable.php';
echo size_readable(dirsize('.'));
?>
</pre>
Output
2645378 2.65 MB
Comments