How can we get directory size in PHP?

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
User avatar
Mds
Forum Contributor
Posts: 110
Joined: Tue Apr 22, 2008 8:56 pm
Contact:

How can we get directory size in PHP?

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How can we get directory size in PHP?

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Mds
Forum Contributor
Posts: 110
Joined: Tue Apr 22, 2008 8:56 pm
Contact:

Re: How can we get directory size in PHP?

Post by Mds »

Hey VladSun. Thank you.
can we get a directory's size by filesize() :?:
Last edited by Mds on Tue Jul 29, 2008 5:43 pm, edited 1 time in total.
User avatar
Mds
Forum Contributor
Posts: 110
Joined: Tue Apr 22, 2008 8:56 pm
Contact:

Re: How can we get directory size in PHP?

Post 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;
}
Post Reply