Jump to content
Sign in to follow this  
Lonelydreamer

Directory Size?

Recommended Posts

Does anyone know of a way to get the size of a directory with the use of a PHP function? I found a fliesize function but I don't know if that works with drectories too.

Share this post


Link to post
Share on other sites

I found this www.alt-php-faq.org/local/25/

but it's lacking an interface so I changed it to include a form. If you need it for a purpose other than what I did with it then you can just use the original.

><form action="" method="post" name="form1">
 <p>Enter Directory Name - Leave empty for a listing of directories </p>
 <p>
   <input name="directory" type="text" id="directory">
 </p>
 <p>
   <input type="submit" name="Submit" value="Submit">
</p>
</form>
<?php

$totalsize=0; 

function show_dir($dir, $pos=2){ 
   global $totalsize; 
   if($pos == 2) 
       echo "<hr><pre>"; 
   $handle = @opendir($dir); 
   while ($file = @readdir ($handle)){ 
       if (eregi("^\.{1,2}$",$file)) 
           continue; 
       if(is_dir($dir.$file)){ 
       echo "|- ".$pos."s <b>$file</b>\n"; 
       show_dir("$dir.$file/", $pos+3); 
   }else{ 
       $size=filesize($dir.$file); 
       echo "|- ".$pos."s $file "; 
       echo("$size <br>"); 
           $totalsize=$totalsize+$size; 
       } 
   } 
   @closedir($handle); 

   if($pos == 2) echo "</pre><hr>"; 

   return($totalsize); 
} 

$totalsize = show_dir("/home/cpanelusername/public_html/$directory/"); 
echo($totalsize); 
?>

Share this post


Link to post
Share on other sites

That is an awesome script. Valuable and can be broken down for just whole directory size, etc. Thanks for the code snippet!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...