我从我的项目中获得了这个代码片段,它在我的指定磁盘中获取可用空间,所以这里是
$ds = disk_total_space(substr(base_path(), 0, 2));
$fs = disk_free_space(substr(base_path(), 0, 2));
那么我需要做的是它一直以 GB 为单位返回值,关于我可以做到这一点的任何想法? 提前非常感谢!
更新我找到了这段代码,它将字节隐藏成不同的格式
if ($ds >= 1073741824)
{
$ds = number_format($ds / 1073741824, 2) . ' GB';
}
elseif ($ds >= 1048576)
{
$ds = number_format($ds / 1048576, 2) . ' MB';
}
elseif ($ds >= 1024)
{
$ds = number_format($ds / 1024, 2) . ' KB';
}
elseif ($ds > 1)
{
$ds = $ds . ' B';
}
elseif ($ds == 1)
{
$ds = $ds . ' B';
}
else
{
$ds = '0 size';
}
关于我如何只能将其制作成 GB 的任何想法?
我在:)使用了这个函数
private function convGB($bytes, $unit = "", $decimals = 2)
{
$units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4,
'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);
$value = 0;
if ($bytes > 0)
{
if (!array_key_exists($unit, $units))
{
$pow = floor(log($bytes)/log(1024));
$unit = array_search($pow, $units);
}
$value = ($bytes/pow(1024,floor($units[$unit])));
}
if (!is_numeric($decimals) || $decimals < 0) {
$decimals = 2;
}
return sprintf('%.' . $decimals . 'f '.$unit, $value);
}
像convGB(*bytes in here*);
一样称呼它
$bytes = disk_total_space("/");
# 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB';
$base = 1024;
echo("<br />" . number_format($bytes / ceil($base ** 3), '2', '.', ' ') . "GB");
# We're assuming that 3 is GB (position 3 of the array)