Directory Size
Posted: Wed Dec 29, 2004 2:45 am
Whats the easiest way to determine the combined size of directory and all subdirs and files in it?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
function rozmiar_katalogu($sciezka) {
if (!is_dir($sciezka)){
return 0;
}
$katalog = opendir($sciezka);
while ($plik = readdir($katalog)){
if (($plik != '.')&&($plik != '..')){
$f = $sciezka.'/'.$plik;
if (is_dir($f)){
$wielkosc+=rozmiar_katalogu($f);
}else{
$wielkosc+=filesize($f);
}
}
}
closedir($katalog);
return $wielkosc;
}
?>Code: Select all
<?php
echo rozmiar_katalogu("mydir");
?>I have ~450 ish folders and at the moment about 8mb contents, but the contents are likely to go for several hundreds MB's. So would that stack space be a problem?feyd wrote:should probably be careful where that's called, since it uses recursion, the stack space could easily run out.
Code: Select all
function directorySize($directory, $ignoreList, $subfolders)
{
if( func_num_args() > 1 && !is_array($ignoreList) )
$ignoreList = array('.', '..');
if( func_num_args() > 2 && !is_bool( $subfolders ) )
$subfolders = false;
if( !is_dir($directory) )
return false;
$directoryList = array($directory);
$len = 1;
$pos = 0;
$size = 0;
while( $pos != $len )
{
$dir =& $directoryListї$pos];
$d = opendir( $dir );
while( false !== ($file = readdir( $d )) )
{
if( !in_array( $file, $ignoreList ) )
{
$file = $dir . $file;
if( is_file( $file )
$size += filesize( $file );
elseif( is_dir( $file ) )
{
$directoryListї] = $file;
++$len;
}
}
}
++$pos;
}
return array($directoryList, $size);
}Code: Select all
function directorySize($directory, $ignoreList, $subfolders)
{
if( func_num_args() > 1 && !is_array($ignoreList) )
$ignoreList = array('.', '..');
if( func_num_args() > 2 && !is_bool( $subfolders ) )
$subfolders = false;
if( !is_dir($directory) )
return false;
$directoryList = array($directory);
$len = 1;
$pos = 0;
$size = 0;
while( $pos != $len )
{
$dir =& $directoryListї$pos];
$d = opendir( $dir );
while( false !== ($file = readdir( $d )) )
{
if( !in_array( $file, $ignoreList ) )
{
$file = $dir . $file;
if( is_file( $file )
$size += filesize( $file );
elseif( $subfolders && is_dir( $file ) )
{
$directoryListї] = $file;
++$len;
}
}
}
++$pos;
}
return array($directoryList, $size);
}