Showing total size of a directory

1

Hello, I would like to know how I can show the total size of a directory and I need the script to be in a folder called status and show the data in the / hd /

I found this code below with everything it just monstrates the occupied space if the file is inside the directory. Someone could help me.

<?php 
    $bytes = disk_free_space("."); 
    $si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
    $base = 1024;
    $class = min((int)log($bytes , $base) , count($si_prefix) - 1);
    echo 'Espa&ccedil;o total do FTP da pasta formatos/hd/ '. sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';
?>
    
asked by anonymous 13.05.2015 / 10:51

1 answer

2

This solution should work on any platform with php 5 or higher.

function GetDirectorySize($path){
    $bytestotal = 0;
    $path = realpath($path);
    if($path!==false){
        foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
            $bytestotal += $object->getSize();
        }
    }
    return $bytestotal;
}
    
13.05.2015 / 13:51