The disk_free_space Function

The disk_free_space function returns the amount of free space in bytes on the specified disk or filesystem. The function accepts one parameter - the path to the disk to check. In Windows, you can specify either a drive letter (for example, 'C:') or a path to a directory. In Unix systems, the path to the mount point is specified.

Syntax

phpdisk_free_space(directory);

Example

Get free space on drive C: in Windows:

php<?php
  $res = disk_free_space('C:');
  echo $res;
?>

Code execution result (example):

php1073741824

Example

Get free space in the root directory of a Unix system:

php<?php
  $res = disk_free_space('/');
  echo $res;
?>

Code execution result (example):

php2147483648

Example

Convert bytes to gigabytes for convenient display:

php<?php
  $bytes = disk_free_space('C:');
  $gb = round($bytes / 1024 / 1024 / 1024, 2);
  echo $gb . ' GB';
?>

Code execution result (example):

php'15.75 GB'

See Also