PHP disk_total_space() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, disk_total_space() returns the total size in bytes of the disk or partition that contains a folder. It tells you how big the disk is, not how much is used.

Use it together with disk_free_space() to show how full a disk is. The result is a float, and it is false if the folder is not valid or the information cannot be read.

Syntax

Call disk_total_space() with the values you need. Start with this line.

PHP
$bytes = disk_total_space(__DIR__);
CallWhat it does
disk_total_space(__DIR__)Returns the total bytes of the disk that holds the script folder.
disk_total_space("/")Returns the total bytes of the root partition (Linux and macOS).
disk_total_space("C:")Returns the total bytes of the C drive (Windows).

The value is the size of the whole disk or partition, even if only part of it is available to your user. Combine it with disk_free_space() to work out the used space.

Library declarations
PHP
disk_total_space(string $directory): float|false

Parameters

ParameterDescription
directoryRequired. A folder on the disk or partition you want to check.

Return Value

Return ValueDescription
floatThe total size of the disk or partition in bytes.
falseReturned if the folder is not valid or the information cannot be read.

PHP Examples

Example 1 — Show the Total Disk Size in GB

Read the total bytes and convert them to gigabytes. Your number will be different.

PHP
<?php
$bytes = disk_total_space(__DIR__);
$gb = $bytes / 1024 / 1024 / 1024;

echo "Total space: ", number_format($gb, 2), " GB", PHP_EOL;

How It Works

  1. disk_total_space(__DIR__) returns the full disk size in bytes.
  2. Dividing by 1024 three times converts the bytes to gigabytes.
  3. number_format() keeps two decimal places.
  4. The size is printed with a label.

Example 2 — Handle an Invalid Folder

A path that does not exist makes disk_total_space() return false.

PHP
<?php
$bytes = @disk_total_space(__DIR__ . "/no-such-folder");

var_dump($bytes);

if ($bytes === false) {
    echo "Folder not found.", PHP_EOL;
}

How It Works

  1. The folder name does not exist on the disk.
  2. The @ operator hides the warning message.
  3. disk_total_space() returns false, which var_dump() shows.
  4. A strict === check lets us print a friendly message.

Example 3 — Calculate Used Space and Percentage

Use total and free space together to show how full the disk is.

PHP
<?php
$total = disk_total_space(__DIR__);
$free = disk_free_space(__DIR__);

$used = $total - $free;
$percent = round($used / $total * 100, 1);

echo "Used: ", $percent, "%", PHP_EOL;

How It Works

  1. disk_total_space() gives the full size of the disk.
  2. disk_free_space() gives the bytes that are still free.
  3. Subtracting free from total gives the used bytes.
  4. We turn the used bytes into a percentage and round it to one decimal place.

Frequently Asked Questions

It returns the total size in bytes of the disk or partition that contains the folder you pass in.
Subtract disk_free_space() from disk_total_space() for the same folder. The result is the number of used bytes.
The folder may not exist, or PHP cannot read the disk information. Always compare the result with false.
No. It returns the size of the whole disk or partition. To get the size of a folder you need to add up the sizes of its files.
Divide the number of bytes by 1024 three times and use number_format() or round() for a clean result.

Next: diskfreespace()

diskfreespace() is an alias of disk_free_space(). It returns how many bytes are free on th.

diskfreespace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful