Reports free disk bytes for the filesystem containing a directory.
What Is disk_free_space() in PHP?
Reports free disk bytes for the filesystem containing a directory.
Its syntax is:
disk_free_space(string $directory): float|false
Parameters:
$directory— Accepted as string; required argument.
Return type: float|false. The value can change immediately after checking; it does not reserve space.
Availability: Depends on filesystem and operating system.
Example 1: Basic Usage
<?php
var_dump(disk_free_space(sys_get_temp_dir()) !== false);
Output:
bool(true)
The value can change immediately after checking; it does not reserve space.
Example 2: A Different Input
<?php
echo disk_free_space(".") !== false ? "Available" : "Unavailable";
Output:
Available
The value can change immediately after checking; it does not reserve space.
Practical Example: Applying It in a Program
<?php
$folderPath = sys_get_temp_dir(); $bytesFree = disk_free_space($folderPath); if ($bytesFree !== false) { echo round($bytesFree / 1024 / 1024) . " MB free"; }
Output:
30538 MB free
The value can change immediately after checking; it does not reserve space.
Important Behavior and Edge Cases
- Key behavior: The value can change immediately after checking; it does not reserve space.
- Environment: Depends on filesystem and operating system.
Comparison With Related Functions
| Function | Purpose |
|---|---|
disk_free_space() | Reports free disk bytes for the filesystem containing a directory. |
disk_total_space() | disk_total_space() reports filesystem capacity; disk_free_space() reports currently free capacity. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: The value can change immediately after checking; it does not reserve space | The value can change immediately after checking; it does not reserve space. |
Key Takeaways
- Reports free disk bytes for the filesystem containing a directory.
- The value can change immediately after checking; it does not reserve space.
- Depends on filesystem and operating system.
Summary: The value can change immediately after checking; it does not reserve space.