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 in 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 above PHP example on disk_total_space() function, is:

php disk total space example

That is, 107427655680 bytes are the total space available in my C drive. Here is the 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 in C drive in GB, then use 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, then 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 disk_total_space() function in PHP, is:

disk_total_space(x)

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

PHP Online Test


« Previous Tutorial Next Tutorial »