Skip to content

Instantly share code, notes, and snippets.

@hnq90
Last active August 29, 2015 14:01
Show Gist options
  • Save hnq90/aac43df061d68620b6d6 to your computer and use it in GitHub Desktop.
Save hnq90/aac43df061d68620b6d6 to your computer and use it in GitHub Desktop.
Calculate size of a file
### Calculate size
function _format_bytes($a_bytes)
{
if ($a_bytes < 1024) {
return $a_bytes . ' B';
} elseif ($a_bytes < 1048576) {
return round($a_bytes / 1024, 2) . ' KB';
} elseif ($a_bytes < 1073741824) {
return round($a_bytes / 1048576, 2) . ' MB';
} elseif ($a_bytes < 1099511627776) {
return round($a_bytes / 1073741824, 2) . ' GB';
} elseif ($a_bytes < 1125899906842624) {
return round($a_bytes / 1099511627776, 2) . ' TB';
} elseif ($a_bytes < 1152921504606846976) {
return round($a_bytes / 1125899906842624, 2) . ' PB';
} elseif ($a_bytes < 1180591620717411303424) {
return round($a_bytes / 1152921504606846976, 2) . ' EB';
} elseif ($a_bytes < 1208925819614629174706176) {
return round($a_bytes / 1180591620717411303424, 2) . ' ZB';
} else {
return round($a_bytes / 1208925819614629174706176, 2) . ' YB';
}
}
### Usage: _format_bytes(filesize($file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment