disk_free_space
disk_free_space(string $directory): float|false
返回目录所在文件系统或磁盘分区的剩余字节数(float),失败返回 false;total 由配套的 disk_total_space 提供。
C C23
#include <stdio.h>
#include <sys/statvfs.h>
int main(void) {
/* statvfs(2) 是 POSIX 的磁盘空间调用——PHP disk_free_space 底层就是它 */
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;
/* 只输出派生事实:绝不预置或打印原始字节值 */
printf("free > 0: %s\n", free_bytes > 0 ? "true" : "false");
printf("free <= total: %s\n", free_bytes <= total_bytes ? "true" : "false");
/* 换算规则用固定示例教学:15728640 字节 = 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) 在 C++ 里同样可用——std 没有封装
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;
// 只输出派生事实:绝不预置或打印原始字节值
std::printf("free > 0: %s\n", free_bytes > 0 ? "true" : "false");
std::printf("free <= total: %s\n", free_bytes <= total_bytes ? "true" : "false");
// 换算规则用固定示例教学:15728640 字节 = 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 查询 Windows 盘符或 Unix 挂载点的剩余空间
var drive = new DriveInfo(Path.GetTempPath());
long free = drive.AvailableFreeSpace;
long total = drive.TotalSize;
// 只输出派生事实:绝不预置或打印原始字节值
Console.WriteLine("free > 0: " + (free > 0));
Console.WriteLine("free <= total: " + (free <= total));
// 换算规则用固定示例教学:15728640 字节 = 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 是 Go 侧对应 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)
// 只输出派生事实:绝不预置或打印原始字节值
fmt.Println("free > 0:", free > 0)
fmt.Println("free <= total:", free <= total)
// 换算规则用固定示例教学:15728640 字节 = 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 替你查询文件系统
File dir = new File(System.getProperty("java.io.tmpdir"));
long free = dir.getFreeSpace();
long total = dir.getTotalSpace();
// 只输出派生事实:绝不预置或打印原始字节值
System.out.println("free > 0: " + (free > 0));
System.out.println("free <= total: " + (free <= total));
// 换算规则用固定示例教学:15728640 字节 = 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+)是 JS 侧对应 disk_free_space 的调用(Unix)
const st = await statfs(tmpdir());
const free = st.bsize * st.bavail;
const total = st.bsize * st.blocks;
// 只输出派生事实:绝不预置或打印原始字节值
console.log("free > 0:", free > 0);
console.log("free <= total:", free <= total);
// 换算规则用固定示例教学:15728640 字节 = 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:目录所在文件系统的剩余字节数
$dir = sys_get_temp_dir();
$free = disk_free_space($dir);
$total = disk_total_space($dir);
// 环境无关的派生事实(绝不预置或打印原始值)
var_dump(is_float($free) && $free > 0);
var_dump(is_float($total) && $total > 0);
var_dump($free <= $total);
// 换算规则用固定示例教学:15728640 字节 = 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 是标准库的磁盘空间读取器(一次调用一个具名元组)
usage = shutil.disk_usage("/tmp") # total, used, free,单位字节
# 只输出派生事实:绝不预置或打印原始字节值
print("free > 0:", usage.free > 0)
print("free <= total:", usage.free <= usage.total)
# 换算规则用固定示例教学:15728640 字节 = 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 是 disk_free_space 底层的原始 POSIX 调用;
// Rust std 没有磁盘空间 API,statvfs/nix crate 是对其的封装
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;
// 只输出派生事实:绝不预置或打印原始字节值
println!("free > 0: {}", free > 0.0);
println!("free <= total: {}", free <= total);
// 换算规则用固定示例教学:15728640 字节 = 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+)是 JS 侧对应 disk_free_space 的调用(Unix)
const st = await statfs(tmpdir());
const free = st.bsize * st.bavail;
const total = st.bsize * st.blocks;
// 只输出派生事实:绝不预置或打印原始字节值
console.log("free > 0:", free > 0);
console.log("free <= total:", free <= total);
// 换算规则用固定示例教学:15728640 字节 = 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
语言差异说明
C:statvfs(2)(<sys/statvfs.h>)即 disk_free_space 的底层——f_frsize × f_bavail 为剩余、f_frsize × f_blocks 为总量;f_bstatvfs 与 f_bfree 的差异是 root 保留块。
C++:同 C 的 statvfs(2)——std 无封装;实战可包一层 RAII 或用 boost::filesystem::space()。
C#:DriveInfo.AvailableFreeSpace / TotalSize——可传盘符(C:\)或 Unix 挂载路径;另有 TotalFreeSpace(含其他用户配额)。
Go:syscall.Statfs(Unix)——Bsize × Bavail 剩余、Bsize × Blocks 总量;Windows 用 golang.org/x/sys/windows 的 GetDiskFreeSpaceEx。
Java:File.getFreeSpace() / getTotalSpace()——JVM 替你查文件系统;注意 getUsableSpace 才是「当前用户可写」的量(root 保留块差异)。
JavaScript:fs.statfs(Node 18.15+,Unix)——返回 bsize/bavail/blocks 原始字段需自乘;异常路径 Promise reject。
PHP:disk_free_space($dir) 返回剩余字节 float、disk_total_space($dir) 返回总量——传目录而非盘符(传文件行为未定义);Windows 下传 C: 或路径皆可;失败返回 false;不适用于远程文件(remote wrapper)。
Python:shutil.disk_usage(path) 原生一次到位——返回 namedtuple(total/used/free),Python 侧最贴合 PHP 双函数的组合。
Rust:std 无磁盘 API——libc::statvfs 裸调(示例)或 statvfs/nix crate;unsafe 块内 zeroed + 填充模式。
TypeScript:同 JavaScript(fs.statfs),类型来自 @types/node 的 StatsFs。