Calculate the size for a directory : Directory « File Directory « PHP






Calculate the size for a directory


<?php
   function directory_size($directory) {
      $directorySize=0;
      if ($dh = @opendir($directory)) {
         while (($filename = readdir ($dh))) {
           if ($filename != "." && $filename != "..") {
             if (is_file($directory."/".$filename)){
                $directorySize += filesize($directory."/".$filename);
             }   
             if (is_dir($directory."/".$filename)){
                $directorySize += directory_size($directory."/".$filename);
             }
           }
        }
      }
      @closedir($dh);
      return $directorySize;
  }

   $directory = "./";
   $totalSize = round((directory_size($directory) / 1024), 2);
   echo "Directory $directory: ".$totalSize. "kb.";

?>

           
       








Related examples in the same category

1.Listing the Contents of a Directory with readdir()
2.readdir: Read entry from directory handle
3.scandir: List files and directories inside the specified path
4.The current working directory
5.Print all files under a directory
6.Read the content from a directory
7.Reading Contents from a Directory
8.Clean Path by Regular Expressions