Forums

PHP

This topic is locked

check folder size

Posted 26 Jan 2007 08:27:02
1
has voted
26 Jan 2007 08:27:02 LorD ExoskeletoN posted:
guyz any help please...any php script that would check folder size?
thank you...hope you can help me

<center>
afraid NOT to FAIL for you learn NOTHING

www.nasbikesphilippines.bravehost.com
<img src="www.motorcyclephilippines.com/forums/signaturepics/sigpic14460_2.gif" border=0></center>

Replies

Replied 29 Jan 2007 17:12:01
29 Jan 2007 17:12:01 Roddy Dairion replied:
&lt;?php
function GetFolderSize($d ="." ) {

$h = @opendir($d);
if($h==0)return 0;

while ($f=readdir($h)){
if ( $f!= ".." {
$sf+=filesize($nd=$d."/".$f);
if($f!="."&&is_dir($nd)){
$sf+=GetFolderSize ($nd);
}
}
}
closedir($h);
return $sf ;
}
$path ="path to your folder";
echo " The Folder Size is : " . GetFolderSize( $path ) . " Bytes";

?&gt;
Replied 07 Feb 2007 02:23:48
07 Feb 2007 02:23:48 LorD ExoskeletoN replied:
@ Roddy Dairion

OT: thank you very much sir...im sorry for it took me so long to give you tnx

more power sir

<center>
afraid NOT to FAIL for you learn NOTHING

www.nasbikesphilippines.bravehost.com
<img src="www.motorcyclephilippines.com/forums/signaturepics/sigpic14460_2.gif" border=0></center>
Replied 05 May 2007 23:14:04
05 May 2007 23:14:04 Alex France replied:
Is there anyway you can make this give the output of the directory size in gigabytes and not bytes?

Thanks Alex
Replied 06 May 2007 13:57:46
06 May 2007 13:57:46 Alex France replied:
It doesnt matter I have just edited the script a little so that it will show in gb instead of bytes.
Here's the script so anybody else can use it:

&lt;?php
function GetFolderSize($d ="." ) {

$h = @opendir($d);
if($h==0)return 0;

while ($f=readdir($h)){
if ( $f!= ".." {
$sf+=filesize($nd=$d."/".$f);
if($f!="."&&is_dir($nd)){
$sf+=GetFolderSize ($nd);
}
}
}
closedir($h);
return $sf ;
}
$path ="your/address/to/the/directory";

//* Here I change the size of the file from bytes to a variable called $foldersize
$foldersize ="" . GetFolderSize( $path ) . "";

//* Here I make the size of 1 Gb in bytes to a variable called $gbsize so I can then divide the size in bytes by the size of a
//*Gigabyte
$gbsize = 1073741824;

//* Here I am combining the two variables and making then divide so I will be left with the size of the directory in Gb
$filesize = $foldersize/$gbsize;

//* This may look stupid but this is the only way I could show the directory size to 2 decimal places
echo " The Directory $path currently has: ";
printf ("%01.2f", $filesize);
echo " Gigabytes of files!";

?&gt;

As you can probably tell I am very inexperienced with php but this works, if you want to show the size of a directory in Mb then all you need to do is change $gbsize to 1048576 and then change the last echo. Thanks Alex

Edited by - alexfrance250291 on 06 May 2007 13:58:59

Edited by - alexfrance250291 on 06 May 2007 14:05:55

Reply to this topic