PHP: Get Folder Size & File Count

Here is a little PHP code you can use to get the size and file count of an entire directory including sub-directories

$filename){
  if($filename!=".." && $filename!="."){
   if(is_dir($dir."/".$filename)){
    $new_foldersize = foldersize($dir."/".$filename);
    $count_size = $count_size + $new_foldersize[0];
    $count = $count + $new_foldersize[1];
   }else if(is_file($dir."/".$filename)){
    $count_size = $count_size + filesize($dir."/".$filename);
    $count++;
   }
  }
  
 }
 
 return array($count_size,$count);
}

$sample = foldersize("SAMPLE_DIRECTORY");

echo "Folder Size : " . $sample[0] . " Bytes 
" ; echo "File Count : " . $sample[1] . " Files " ; ?>

5 thoughts on “PHP: Get Folder Size & File Count”

  1. Hey man, great snippet. I don’t usually deal with server directory access or files in this manner and need something that would just count files for dynamic access. Thanks again!

  2. Pingback: Haukur H.

Leave a Reply

Your email address will not be published. Required fields are marked *