byte to gb php code example
Example 1: convert gb to bytes php
<?php
function tobytes($size, $type)
{
$types = array("B", "KB", "MB", "GB", "TB", "PB");
if($key = array_search($type, $types))
return $size * pow(1024, $key);
else return "invalid type";
}
echo tobytes(15, "MB");
echo tobytes(2, "KB");
echo tobytes(3, "w/e");
?>
Example 2: convert byte to megabyte php
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
return round($bytes, $precision) . ' ' . $units[$pow];
}