PHP获取当前磁盘的总容量与剩余空间
该应用用到了PHP的自带函数
disk_total_space 获取磁盘的容量
disk_free_space 获取磁盘的剩余空间
<?php
//PHP获取磁盘大小
function get_disk_total(int $total) : string
{
$config = [
'3' => 'GB',
'2' => 'MB',
'1' => 'KB'
];
foreach($config as $key => $value){
if($total > pow(1024, $key)){
return round($total / pow(1024,$key)).$value;
}
return $total . 'B';
}
}
//
echo '当前磁盘的总容量:' . get_disk_total(disk_total_space('.'));
echo '<hr/>';
echo '当前磁盘的剩余空间:' . get_disk_total(disk_free_space('.')) ;
本文介绍了一种使用PHP内置函数disk_total_space和disk_free_space来获取当前磁盘总容量及剩余空间的方法。通过自定义函数get_disk_total,将字节单位转换为更易读的GB、MB或KB等单位。
99

被折叠的 条评论
为什么被折叠?



