Directory Size
Moderator: General Moderators
-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
Directory Size
Whats the easiest way to determine the combined size of directory and all subdirs and files in it?
Complete function:
Usage:
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");
?>-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
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.
Is there non-recurseve version of this?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
something like this
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);
}-
Shendemiar
- Forum Contributor
- Posts: 404
- Joined: Thu Jan 08, 2004 8:28 am
How does it work?
I need to pass arrays?
In general, can i not pass some variable to function?
like
function some($a,$b,$c)
{
}
and then
$x=some(1,,2);
I need to pass arrays?
In general, can i not pass some variable to function?
like
function some($a,$b,$c)
{
}
and then
$x=some(1,,2);
Last edited by Shendemiar on Wed Dec 29, 2004 6:50 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
the minimum required for the function is the first variable. The second variable is an optional array of items to ignore (both files and directories). The third is expected to be boolean, for searching subfolders or not.. (sorry, I forgot to add that control in, it searches subfolders always right now) ... If you don't want to send the second, but do want to send the third, pass something other than an array.
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);
}