<?php
$currentPagePath = __DIR__; // 获取当前页面所在的路径 如果检测其他盘可改 $diskPath = 'D:';
// 提取盘符部分
$driveLetter = strtoupper(substr($currentPagePath, 0, 1));
// 获取总空间大小(字节)
$totalSpaceBytes = disk_total_space($driveLetter . ':');
// 将字节转换为GB
$totalSpaceGB = round($totalSpaceBytes / (1024 * 1024 * 1024), 2);
// 获取已使用空间大小(字节)
$usedSpaceBytes = $totalSpaceBytes - disk_free_space($driveLetter . ':');
// 将字节转换为GB
$usedSpaceGB = round($usedSpaceBytes / (1024 * 1024 * 1024), 2);
// 计算剩余空间大小(字节)
$freeSpaceBytes = disk_free_space($driveLetter . ':');
// 将字节转换为GB
$freeSpaceGB = round($freeSpaceBytes / (1024 * 1024 * 1024), 2);
// 打印结果
echo "总空间大小:{$totalSpaceGB}GB<br>";
echo "已使用空间:{$usedSpaceGB}GB<br>";
echo "剩余空间:{$freeSpaceGB}GB";
效果图如下