PHP disk_free_space() | Find Amount of Free Disk Space

The PHP disk_free_space() function is used when we need to find the free space available in specified disk or filesystem. The space returned using this function will be in bytes, by default. For example:

<?php
   $space = disk_free_space("C:");
   echo $space;
?>

The output of previous example is:

php disk free space example

That is, 67981537280 bytes. Here is the snapshot of the C drive of my computer system:

php disk space

PHP Find Amount of Free Disk Space in GB

To modify the above program in a way to print the amount of free space available in C drive in GB, then use following example:

<?php
   $spaceBytes = disk_free_space("C:");
   
   $spaceKb = $spaceBytes/1024;
   $spaceMb = $spaceKb/1024;
   $spaceGb = $spaceMb/1024;
   
   echo "<p>Free Space available in <b>C</b> Drive is <b>$spaceGb</b> GB</p>";
?>

Now the output should be:

php find free space available in disk

To remove all digits after decimals, then use (int) before $spaceGb. For example:

<?php
   $spaceBytes = disk_free_space("C:");
   
   $spaceGb = $spaceBytes/1024/1024/1024;
   $spaceGb = (int)$spaceGb;
   
   echo "<p><b>C</b> Drive Free Space = <b>$spaceGb</b> GB</p>";
?>

Now the output should be:

php disk free space function

PHP disk_free_space() Syntax

The syntax of disk_free_space() function in PHP, is:

disk_free_space(x)

The x parameter is required, refers to the filesystem or disk whose free space we need to find/see.

PHP Online Test


« Previous Tutorial Next Tutorial »