Skip to content

Instantly share code, notes, and snippets.

@2ec0b4
Created November 1, 2014 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2ec0b4/90b68252e90140eb5ce6 to your computer and use it in GitHub Desktop.
Save 2ec0b4/90b68252e90140eb5ce6 to your computer and use it in GitHub Desktop.
Convert bytes
<?php
/**
* Convert bytes
* @param int|string $bytes
* @return mixed returns an array with the converted value and a label key (see $unit)
*/
function convert_bytes($bytes)
{
$unit = array(
'label_file_b_size',
'label_file_kb_size',
'label_file_mb_size',
'label_file_gb_size',
);
if( empty($bytes) ) {
return array(0, $unit[0]);
}
return array(@round($bytes/pow(1024,($i=floor(log($bytes,1024)))),2), $unit[$i]);
}
list($value, $unit) = convert_bytes(118218);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment