Page 1 of 1

How can we get directory size in PHP?

Posted: Tue Jul 29, 2008 7:54 am
by Mds
Hi there.
I want to get a directory size by PHP.
for example I used this code :

Code: Select all

$size = disk_total_space("../upload")/ 1024;
echo $size;
but I saw this warning :

Code: Select all

Warning: disk_total_space() [function.disk-total-space]: The system cannot find the path specified. in H:\Apache2.2\htdocs\c-panel\cp_folders.php on line 88
I'm sure that there is the folder in "../upload".

What's wrong with it and how I can solve it ?
thanks in advance

Re: How can we get directory size in PHP?

Posted: Tue Jul 29, 2008 8:08 am
by VladSun
I suppose you are using Windows:
I think you may have misunderstood - given a directory, this function tells you how big the disk partition is that the directory exists on.

So disk_total_space("C:\Windows\") will tell you how big drive C is.
Look at filesize() php man page - there are some useful comments about calculating directory size.

Re: How can we get directory size in PHP?

Posted: Tue Jul 29, 2008 8:17 am
by Mds
Hey VladSun. Thank you.
can we get a directory's size by filesize() :?:

Re: How can we get directory size in PHP?

Posted: Tue Jul 29, 2008 9:24 am
by Mds
Thank you VladSun.
I didn't see carefully .
I found this function :

Code: Select all

 
function dirsize($dir,$buf=2)
{
  static $buffer;
  if(isset($buffer[$dir]))
    return $buffer[$dir];
  if(is_file($dir))
    return filesize($dir);
  if($dh=opendir($dir))
  {
    $size=0;
    while(($file=readdir($dh))!==false)
    {
      if($file=='.' || $file=='..')
        continue;
      $size+=dirsize($dir.'/'.$file,$buf-1);
    }
    closedir($dh);
    if($buf>0)
      $buffer[$dir]=$size;
    return $size;
  }
  return false;
}