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 on a 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 the previous example is:

php disk free space example

That is, 67981537280 bytes. Here is a 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 on the "C" drive in "GB," then use the 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, 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 the disk_free_space() function in PHP is:

disk_free_space(x)

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

Advantages of the disk_free_space() function in PHP

Disadvantages of the disk_free_space() function in PHP

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!