Directory Size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Directory Size

Post by Shendemiar »

Whats the easiest way to determine the combined size of directory and all subdirs and files in it?
gskaruz
Forum Newbie
Posts: 8
Joined: Mon Mar 15, 2004 6:45 am

Post 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");

?>
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Thanks fellow!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

should probably be careful where that's called, since it uses recursion, the stack space could easily run out.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
}
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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);
Last edited by Shendemiar on Wed Dec 29, 2004 6:50 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
}
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

) missing on line 26
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

as I said, untested.. :P and it's line 24 that's missing it. :P
Post Reply