PHP disk_free_space() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, disk_free_space() returns how many bytes are free on the disk or partition that contains a folder. You can use it to check there is enough room before saving a big file.

The result is a float because disk sizes can be bigger than a normal integer on some systems. It returns false if the folder does not exist or the information cannot be read.

Syntax

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

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

diskfreespace() is an alias of this function. The value shows space available to the current user, which may be lower than the real free space if disk quotas are used.

Library declarations
PHP
disk_free_space(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 Disk Space in GB

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

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

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

How It Works

  1. disk_free_space(__DIR__) returns the free bytes on the current disk.
  2. We divide by 1024 three times to convert bytes to gigabytes.
  3. number_format() rounds the number to two decimal places.
  4. The result is printed with a label.

Example 2 — Handle an Invalid Folder

A folder that does not exist makes disk_free_space() return false. Check for it before using the value.

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

if ($bytes === false) {
    echo "Could not read the disk space.", PHP_EOL;
} else {
    echo "Free: ", $bytes, PHP_EOL;
}

How It Works

  1. The folder name points to a path that does not exist.
  2. The @ operator hides the warning that PHP would print.
  3. disk_free_space() returns false, so we use === to compare strictly.
  4. The script prints a friendly message.

Example 3 — Check Space Before Saving a File

Compare the free space with the size of the data you want to save, and add a small safety margin.

PHP
<?php
$data = str_repeat("x", 1048576);
$needed = strlen($data) + 1048576;

$free = disk_free_space(__DIR__);

if ($free !== false && $free > $needed) {
    file_put_contents(__DIR__ . "/big.txt", $data);
    echo "Saved ", strlen($data), " bytes", PHP_EOL;
    unlink(__DIR__ . "/big.txt");
} else {
    echo "Not enough space.", PHP_EOL;
}

How It Works

  1. We build 1 MB of data and add another 1 MB as a safety margin.
  2. disk_free_space() tells us how much room is left.
  3. If the free space is larger than needed, we save the file.
  4. unlink() removes the demo file, and an else branch handles a full disk.

Frequently Asked Questions

It returns the number of free bytes on the disk or partition that holds the folder you give it.
Divide the result by 1024 for KB, again for MB, and again for GB. Use number_format() or round() to make it easy to read.
The folder may not exist, or PHP may not be allowed to read the disk information. Always compare the result with false.
disk_free_space() returns the bytes that are still available, and disk_total_space() returns the full size of the disk or partition.
Yes. diskfreespace() is an alias of disk_free_space(), but the name with the underscore is the one you should use.

Next: disk_total_space()

disk_total_space() returns the total size in bytes of the disk or partition that contains .

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