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
Mds
Forum Contributor
Posts: 110 Joined: Tue Apr 22, 2008 8:56 pm
Contact:
Post
by Mds » Tue Jul 29, 2008 7:54 am
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
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Jul 29, 2008 8:08 am
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
Mds
Forum Contributor
Posts: 110 Joined: Tue Apr 22, 2008 8:56 pm
Contact:
Post
by Mds » Tue Jul 29, 2008 8:17 am
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.
Mds
Forum Contributor
Posts: 110 Joined: Tue Apr 22, 2008 8:56 pm
Contact:
Post
by Mds » Tue Jul 29, 2008 9:24 am
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;
}