See available disk space in PHP on a web server

1

I tried to fetch the available space on the disk but it gets a lush number (124GB transformed) while it only has 12GB free. Here's what I used.

<?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 $bytes . '<br />';
    echo sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';
?>
    
asked by anonymous 03.05.2018 / 20:15

1 answer

0

Try to create a function to transform the bytes value that is returned by disk_free_space and display on screen.

It would look like this:

<?php 
function formatBytes($bytes, $precision = 2) 
{ 
    $base = log($bytes, 1024);
    $suffixes = array('', 'K', 'M', 'G', 'T');   

    return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}

$bytes = disk_free_space("."); 
echo formatBytes($bytes,2);

?>

Source:

    
03.05.2018 / 20:35