Library code snippets

Total size of a directory

The following code includes a function to calculate the total hard disk space taken up by a directory.

<?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("c:/winnt/system32/");
echo($totalsize);
?>

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Total size of a directory.

Leave a comment

Sign in or Join us (it's free).

 altphpfaq

Related discussion

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Dec 3

    The Auckland PHP December meetup

    Auckland, New Zealand

    Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.

Want to stay in touch with what's going on? Follow us on twitter!