Showing total size of a directory

Asked

Viewed 1,340 times

1

Hello 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 of the format/hd folder/.

I found this code below with everything it only shows 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 />';
?>
  • Do you need the total size of the directory or the free disk space? Because the disk_free_space() function returns the free space and not the occupied space.

  • In case then eats would be to put the space occupied ?

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;
}
  • 1

    Thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.