disk_free_space
disk_free_space(string $directory): float|false
Returns the bytes free on the filesystem or partition holding the directory as a float, false on failure; disk_total_space supplies the total.
C C23
#include <stdio.h>
#include <sys/statvfs.h>
int main(void) {
/* statvfs(2) is the POSIX disk space call - PHP disk_free_space wraps it */
struct statvfs fs;
if (statvfs("/tmp", &fs) != 0) {
return 1;
}
double free_bytes = (double)fs.f_frsize * fs.f_bavail;
double total_bytes = (double)fs.f_frsize * fs.f_blocks;
/* Only derived facts go to output: never preset or printed raw values */
printf("free > 0: %s\n", free_bytes > 0 ? "true" : "false");
printf("free <= total: %s\n", free_bytes <= total_bytes ? "true" : "false");
/* Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB */
long b = 15728640;
const char *units[] = {"B", "KiB", "MiB", "GiB"};
int i = 0;
while (b >= 1024 && i < 3) { b /= 1024; i++; }
printf("fixed example: %ld %s\n", b, units[i]);
return 0;
}
free > 0: true free <= total: true fixed example: 15 MiB
C++ C++26
#include <cstdio>
#include <sys/statvfs.h>
int main() {
// statvfs(2) works in C++ too - no std wrapper exists
struct statvfs fs;
if (statvfs("/tmp", &fs) != 0) {
return 1;
}
double free_bytes = static_cast<double>(fs.f_frsize) * fs.f_bavail;
double total_bytes = static_cast<double>(fs.f_frsize) * fs.f_blocks;
// Only derived facts go to output: never preset or printed raw values
std::printf("free > 0: %s\n", free_bytes > 0 ? "true" : "false");
std::printf("free <= total: %s\n", free_bytes <= total_bytes ? "true" : "false");
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
long b = 15728640;
const char *units[] = {"B", "KiB", "MiB", "GiB"};
int i = 0;
while (b >= 1024 && i < 3) { b /= 1024; i++; }
std::printf("fixed example: %ld %s\n", b, units[i]);
return 0;
}
free > 0: true free <= total: true fixed example: 15 MiB
C# v11.0.100
// DriveInfo queries free space on a Windows drive letter or Unix mount
var drive = new DriveInfo(Path.GetTempPath());
long free = drive.AvailableFreeSpace;
long total = drive.TotalSize;
// Only derived facts go to output: never preset or printed raw values
Console.WriteLine("free > 0: " + (free > 0));
Console.WriteLine("free <= total: " + (free <= total));
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
long b = 15728640;
string[] units = { "B", "KiB", "MiB", "GiB" };
int i = 0;
while (b >= 1024 && i < 3) { b /= 1024; i++; }
Console.WriteLine("fixed example: " + b + " " + units[i]);
free > 0: True free <= total: True fixed example: 15 MiB
Go v1.27.4
package main
import (
"fmt"
"syscall"
)
func main() {
// syscall.Statfs is the Go call behind disk_free_space (Unix)
var st syscall.Statfs_t
if err := syscall.Statfs("/tmp", &st); err != nil {
panic(err)
}
free := float64(st.Bsize) * float64(st.Bavail)
total := float64(st.Bsize) * float64(st.Blocks)
// Only derived facts go to output: never preset or printed raw values
fmt.Println("free > 0:", free > 0)
fmt.Println("free <= total:", free <= total)
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
b := 15728640
units := []string{"B", "KiB", "MiB", "GiB"}
i := 0
for b >= 1024 && i < 3 {
b /= 1024
i++
}
fmt.Println("fixed example:", b, units[i])
}
free > 0: true free <= total: true fixed example: 15 MiB
Java v27.0.1
import java.io.File;
public class Main {
public static void main(String[] args) {
// File.getFreeSpace/getTotalSpace: JVM queries the filesystem for you
File dir = new File(System.getProperty("java.io.tmpdir"));
long free = dir.getFreeSpace();
long total = dir.getTotalSpace();
// Only derived facts go to output: never preset or printed raw values
System.out.println("free > 0: " + (free > 0));
System.out.println("free <= total: " + (free <= total));
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
long b = 15728640;
String[] units = {"B", "KiB", "MiB", "GiB"};
int i = 0;
while (b >= 1024 && i < 3) { b /= 1024; i++; }
System.out.println("fixed example: " + b + " " + units[i]);
}
}
free > 0: true free <= total: true fixed example: 15 MiB
JavaScript v26.13.1
import { statfs } from "node:fs/promises";
import { tmpdir } from "node:os";
// fs.statfs (Node 18.15+) is the JS call behind disk_free_space (Unix)
const st = await statfs(tmpdir());
const free = st.bsize * st.bavail;
const total = st.bsize * st.blocks;
// Only derived facts go to output: never preset or printed raw values
console.log("free > 0:", free > 0);
console.log("free <= total:", free <= total);
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
let b = 15728640;
const units = ["B", "KiB", "MiB", "GiB"];
let i = 0;
while (b >= 1024 && i < 3) { b = Math.floor(b / 1024); i++; }
console.log("fixed example:", b, units[i]);
free > 0: true free <= total: true fixed example: 15 MiB
PHP v8.6.0
// disk_free_space: bytes free on the filesystem holding the directory
$dir = sys_get_temp_dir();
$free = disk_free_space($dir);
$total = disk_total_space($dir);
// Environment-independent derived facts (never preset or printed values)
var_dump(is_float($free) && $free > 0);
var_dump(is_float($total) && $total > 0);
var_dump($free <= $total);
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
$b = 15728640.0;
$units = ['B', 'KiB', 'MiB', 'GiB'];
$i = 0;
while ($b >= 1024.0 && $i < 3) { $b /= 1024.0; $i++; }
printf("fixed example: %.0f %s\n", $b, $units[$i]);
bool(true) bool(true) bool(true) fixed example: 15 MiB
Python v3.15.2
import shutil
# shutil.disk_usage is the stdlib disk space reader (one call, one namedtuple)
usage = shutil.disk_usage("/tmp") # total, used, free in bytes
# Only derived facts go to output: never preset or printed raw values
print("free > 0:", usage.free > 0)
print("free <= total:", usage.free <= usage.total)
# Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
b = 15728640
units = ["B", "KiB", "MiB", "GiB"]
i = 0
while b >= 1024 and i < 3:
b //= 1024
i += 1
print("fixed example:", b, units[i])
free > 0: True free <= total: True fixed example: 15 MiB
Rust v1.99.1
fn main() {
// libc::statvfs is the raw POSIX call behind disk_free_space;
// Rust std has no disk-space API, the statvfs/nix crates wrap this
let st = unsafe {
let mut st: libc::statvfs = std::mem::zeroed();
let path = std::ffi::CString::new("/tmp").unwrap();
if libc::statvfs(path.as_ptr(), &mut st) != 0 {
panic!("statvfs failed");
}
st
};
let free = st.f_frsize as f64 * st.f_bavail as f64;
let total = st.f_frsize as f64 * st.f_blocks as f64;
// Only derived facts go to output: never preset or printed raw values
println!("free > 0: {}", free > 0.0);
println!("free <= total: {}", free <= total);
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
let mut b: u64 = 15728640;
let units = ["B", "KiB", "MiB", "GiB"];
let mut i = 0;
while b >= 1024 && i < 3 {
b /= 1024;
i += 1;
}
println!("fixed example: {} {}", b, units[i]);
}
free > 0: true free <= total: true fixed example: 15 MiB
TypeScript v26.13.1
import { statfs } from "node:fs/promises";
import { tmpdir } from "node:os";
// fs.statfs (Node 18.15+) is the JS call behind disk_free_space (Unix)
const st = await statfs(tmpdir());
const free = st.bsize * st.bavail;
const total = st.bsize * st.blocks;
// Only derived facts go to output: never preset or printed raw values
console.log("free > 0:", free > 0);
console.log("free <= total:", free <= total);
// Scale conversion taught on a FIXED example: 15728640 bytes = 15 MiB
let b = 15728640;
const units = ["B", "KiB", "MiB", "GiB"];
let i = 0;
while (b >= 1024 && i < 3) { b = Math.floor(b / 1024); i++; }
console.log("fixed example:", b, units[i]);
free > 0: true free <= total: true fixed example: 15 MiB
Language differences
C: statvfs(2) (<sys/statvfs.h>) is what disk_free_space wraps - f_frsize × f_bavail is free, f_frsize × f_blocks is total; f_bfree vs f_bavail differ by root-reserved blocks.
C++: same statvfs(2) as C - no std wrapper; wrap it in RAII or use boost::filesystem::space() in real work.
C#: DriveInfo.AvailableFreeSpace / TotalSize - accepts a drive letter (C:\) or a Unix mount path; TotalFreeSpace also exists (includes other users' quotas).
Go: syscall.Statfs (Unix) - Bsize × Bavail free, Bsize × Blocks total; on Windows use GetDiskFreeSpaceEx from golang.org/x/sys/windows.
Java: File.getFreeSpace() / getTotalSpace() - the JVM queries the filesystem for you; note getUsableSpace is the "writable by this user" figure (root-reserved blocks).
JavaScript: fs.statfs (Node 18.15+, Unix) - returns raw bsize/bavail/blocks fields you multiply yourself; failure path rejects the Promise.
PHP: disk_free_space($dir) returns free bytes as a float and disk_total_space($dir) the total - pass a directory, not a drive letter (a file path has undefined behavior); on Windows both C: and paths work; false on failure; remote files are not supported (remote wrappers).
Python: shutil.disk_usage(path) does it natively in one call - returns a namedtuple (total/used/free), the closest match to PHP's pair of functions.
Rust: no disk API in std - raw libc::statvfs (example) or the statvfs/nix crates; zeroed-then-fill pattern inside an unsafe block.
TypeScript: same as JavaScript (fs.statfs), with the StatsFs type from @types/node.