0

I am very confused on the output of disk_free_space vs. df

Some examples:

For a drive that is local:

php -r "print disk_free_space('/Users');"
124647247872

df /Users/
Filesystem 512-blocks      Used       Available       Capacity  iused          ifree           %iused  Mounted on
/dev/disk1  974716928 730754360 243450568    76%         91408293  30431321   75%   /

243450568 * 512 (output of df) = 124646690816 which is reasonably similar to the output of php which is 124647247872

However, lets take an example of an NFS mounted external drive

192.168.1.10:/homestorage on /media (nfs, nodev, nosuid, automounted, nobrowse)

php -r "print disk_free_space('/media');"
1879997071360


df /media
Filesystem                 512-blocks       Used  Available Capacity iused     ifree %iused  Mounted on
192.168.1.10:/homestorage 11604767296 3637930720 7966836576    32%  192244 182620428    0%   /media

The results of df 7966836576 * 512 are not even remotely closed to disk_free_space result of 1879997071360

(Note: I did read another SO thread here - but that does not address the issue here, or so I think)

The PHP manual for disk free space says that it doesn't work for "remote files" but what constitutes a remote file? The examples show http urls. In *nix they are mounted as local FS, so what exactly is messing things up and can disk_free_space be reliably used for mounted drives at all?

Thank you.

Community
  • 1
  • 1
user1361529
  • 2,667
  • 29
  • 61
  • If you check out php-src (https://github.com/php/php-src/blob/71c19800258ee3a9548af9a5e64ab0a62d1b1d8e/ext/standard/filestat.c#L335) you'll find out it uses C's `statfs` internally on anything other than Windows or OS2. That means you can reduce your issue to "difference between statfs and df". I found this: http://stackoverflow.com/questions/21693673/differ-in-output-of-df-and-statfs – Sergiu Paraschiv Feb 10 '16 at 17:02
  • And this: http://stackoverflow.com/questions/4965355/converting-statvfs-to-percentage-free-correctly – Sergiu Paraschiv Feb 10 '16 at 17:03
  • Correction: it tries using `statvfs` if it's available, but both should be pretty similar, only the data structure returned is different: http://stackoverflow.com/questions/1653163/difference-between-statvfs-and-statfs-system-calls – Sergiu Paraschiv Feb 10 '16 at 17:05
  • Hi Sergiu, yes I did see it uses stafvfs. I also just read the other threads - my issue is the results are reasonably accurate for a local drive but hugely inaccurate for mounted drives. I'm curious about this discrepancy. Thank you. – user1361529 Feb 10 '16 at 17:08
  • There are bug reports about this discrepancy everywhere. From PHP or df themselves to specific Linux distros. Best bet would be to find out the exact version and distribution you use and check their bug tracker, most likely you'll find relevant information there. I'm pretty sure this is not a PHP bug though. – Sergiu Paraschiv Feb 10 '16 at 17:25

0 Answers0