Page 1 of 1

Directory Size

Posted: Wed Dec 29, 2004 2:45 am
by Shendemiar
Whats the easiest way to determine the combined size of directory and all subdirs and files in it?

Posted: Wed Dec 29, 2004 5:31 am
by gskaruz
Complete function:

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; 
}
?>
Usage:

Code: Select all

<?php

echo rozmiar_katalogu("mydir");

?>

Posted: Wed Dec 29, 2004 5:39 am
by Shendemiar
Thanks fellow!

Posted: Wed Dec 29, 2004 8:03 am
by feyd
should probably be careful where that's called, since it uses recursion, the stack space could easily run out.

Posted: Wed Dec 29, 2004 9:32 am
by Shendemiar
feyd wrote:should probably be careful where that's called, since it uses recursion, the stack space could easily run out.
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?

Is there non-recurseve version of this?

Posted: Wed Dec 29, 2004 10:39 am
by feyd
something like this

Code: Select all

function directorySize($directory, $ignoreList, $subfolders)
{
  if( func_num_args() &gt; 1 &amp;&amp; !is_array($ignoreList) )
    $ignoreList = array('.', '..');
  if( func_num_args() &gt; 2 &amp;&amp; !is_bool( $subfolders ) )
    $subfolders = false;
  if( !is_dir($directory) )
    return false;

  $directoryList = array($directory);
  $len = 1;
  $pos = 0;
  $size = 0;

  while( $pos != $len )
  {
    $dir =&amp; $directoryList&#1111;$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&#1111;] = $file;
          ++$len;
        }
      }
    }

    ++$pos;
  }

  return array($directoryList, $size);
}

Posted: Wed Dec 29, 2004 5:08 pm
by Shendemiar
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);

Posted: Wed Dec 29, 2004 6:47 pm
by feyd
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() &gt; 1 &amp;&amp; !is_array($ignoreList) )
    $ignoreList = array('.', '..');
  if( func_num_args() &gt; 2 &amp;&amp; !is_bool( $subfolders ) )
    $subfolders = false;
  if( !is_dir($directory) )
    return false;

  $directoryList = array($directory);
  $len = 1;
  $pos = 0;
  $size = 0;

  while( $pos != $len )
  {
    $dir =&amp; $directoryList&#1111;$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 &amp;&amp; is_dir( $file ) )
        {
          $directoryList&#1111;] = $file;
          ++$len;
        }
      }
    }

    ++$pos;
  }

  return array($directoryList, $size);
}

Posted: Thu Dec 30, 2004 7:28 am
by Shendemiar
) missing on line 26

Posted: Thu Dec 30, 2004 9:03 am
by feyd
as I said, untested.. :P and it's line 24 that's missing it. :P