Search code examples
phpdiskspacelinux-disk-free

PHP Disk Used Space - Dependency


I'm using php to try and work out the total, available & used space on a disk.

This is the code I'm running :

$total = (disk_total_space("/")/1024);
$available = (disk_free_space("/")/1024);
$used = ($total - $available);

echo "$total\n\n";
echo "$available\n\n";
echo "$used\n\n";

and the output returned :

305594616

293030828

12563788

A df of the same partition returns the same values of the Total and Available, but used is different.

 df /
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda2      305594616 11632392 293030828   4% /

Why does php show 12563788 and df show 11632392 ?

Is there anyway to get accurate values ?

Thanks


Solution

  • Assuming you use some ext file system, there is by default an area reserved for root user (5%, can be checked or changed with the tune2fs tool, for example). This is why total, used and free don't match up.

    There is no reliable way with PHP functions to get the used space at the moment.