View available disk space in PHP on a web server

Asked

Viewed 119 times

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 />';
?>

1 answer

0

Try creating a function to transform the byte value returned by disk_free_space and display on screen.

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:

https://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

  • I’ll try that, I’ll inform you soon

  • Keep saying 124GB

  • Echo the disk_free_space function and tell us how many bytes it returns

Browser other questions tagged

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