Usage of disk_free_space() and disk_total_space() in PHP

4.5k views Asked by At

I'm not clear on what disk_free_space and disk_total_space are supposed to do.

The documentation says "Returns available space on filesystem or disk partition" and "Returns the total size of a filesystem or disk partition", respectively. Meanwhile, the argument is described, in each case, as "A directory of the filesystem or disk partition."

So am I able to get the available disk space only for a partition or also for a specific folder?

In other words, is it normal that both parts of this script return the same values?

<?php 

$path = '/home/bilingueanglais'; // an estimated 14 GB according to Webmin
$free = floor( disk_free_space( $path ) / ( 1024 * 1024 ) );
$total = floor( disk_total_space( $path ) / (1024 * 1024 ) );
echo 'Diskspace for path `' . $path . '`:<br>';
echo $free . 'MB out of ' . $total . ' MB<br><br><br>';

$path = '/home/bilingueanglais/public_html/assets/theme'; // just 225 KB in reality
$free = floor( disk_free_space( $path ) / ( 1024 * 1024 ) );
$total = floor( disk_total_space( $path ) / (1024 * 1024 ) );
echo 'Diskspace for path `' . $path . '`:<br>';
echo $free . 'MB out of ' . $total . ' MB<br><br><br>';

?>
2

There are 2 answers

0
Stephen On BEST ANSWER

It's returning the same values because those functions specifically look at the volume free/total capacity respectively.

Think of it like the output of df on Linux/OS X/etc - it shows disk/volume sizes not directory sizes.

0
Sammitch On

It's a *nix convention. Let's say you've got a filesystem mounted on /var/www:

disk_free_space('/var/www'); will return the same result as disk_free_space('/var/www/foobar.com'); because they are the same filesystem and that's what the function reporting.

What you're running is likely reporting the disk usage of the entire server, not just your user.