PHP diskfreespace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, diskfreespace() is an alias of disk_free_space(). It returns how many bytes are free on the disk or partition that contains a folder.

It behaves exactly like disk_free_space(), so the two names can be used in the same way. New code should use disk_free_space() because the name is easier to read.

Syntax

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

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

diskfreespace() is only an alias and works the same as disk_free_space(). Prefer the underscore name in new code, and read the free space as a float of bytes.

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

Parameters

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

Return Value

Return ValueDescription
floatThe number of free bytes.
falseReturned if the folder is not valid or the information cannot be read.

PHP Examples

Example 1 — Show Free Space with the Alias

Use diskfreespace() to read the free bytes and show them in gigabytes. Your number will be different.

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

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

How It Works

  1. diskfreespace(__DIR__) returns the free bytes on the current disk.
  2. We divide by 1024 three times to get gigabytes.
  3. number_format() rounds the value to two decimals.
  4. The text is printed with a label.

Example 2 — The Alias Matches the Original

Both names return the same value, which proves they are one function.

PHP
<?php
$a = diskfreespace(__DIR__);
$b = disk_free_space(__DIR__);

var_dump(abs($a - $b) < 1048576);

var_dump(@diskfreespace(__DIR__ . "/no-such-folder"));

How It Works

  1. We call the alias and the original function for the same folder.
  2. The values are almost the same, so we allow a small difference for files changing on the disk.
  3. The first var_dump() prints true.
  4. A missing folder makes the alias return false, just like disk_free_space().

Example 3 — Format Bytes as a Readable Size

Write a small helper that turns bytes into KB, MB, or GB, and use it with diskfreespace().

PHP
<?php
function formatBytes(float $bytes): string
{
    $units = ["B", "KB", "MB", "GB", "TB"];
    $i = 0;

    while ($bytes >= 1024 && $i < count($units) - 1) {
        $bytes /= 1024;
        $i++;
    }

    return round($bytes, 2) . " " . $units[$i];
}

echo "Free: ", formatBytes(diskfreespace(__DIR__)), PHP_EOL;

How It Works

  1. The function keeps a list of units from B to TB.
  2. The while loop divides by 1024 until the number is small enough.
  3. Each division moves to the next unit in the list.
  4. diskfreespace() supplies the bytes and the helper prints a clean size.

Frequently Asked Questions

It is an alias of disk_free_space(). It returns the number of free bytes on the disk or partition of a folder.
No, it is still available as an alias. Most code uses disk_free_space() because the name is clearer.
It returns false if the folder is not valid or the disk information cannot be read. Compare with === false.
Divide the bytes by 1024 three times, and use number_format() or round() for a short readable number.
diskfreespace() gives the free bytes, while disk_total_space() gives the full size of the disk.

Next: fclose()

fclose() closes a file that was opened with fopen(). It tells PHP you are finished with th.

fclose() 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