PHP disk_total_space(): Find total disk space

The PHP disk_total_space() function is used when we need to find the total 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_total_space("C:");
   echo $space;
?>

The output of the above PHP example on the disk_total_space() function is:

php disk total space example

That is, 107427655680 bytes are the total space available on my C drive. Here is a snapshot of the C drive of my computer system:

php disk space

Find Amount of Total Disk Space in GB using PHP

To modify the above program to print the total space available on the C drive in GB, then use the following example:

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

Now the output should be:

php find total space available in disk

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

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

Now the output should be:

php disk total space function

PHP disk_total_space() Syntax

The syntax of the disk_total_space() function in PHP is:

disk_total_space(x)

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

Advantages of the disk_total_space() function in PHP

Disadvantages of the disk_total_space() function in PHP

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!