Created
November 1, 2014 14:53
-
-
Save 2ec0b4/90b68252e90140eb5ce6 to your computer and use it in GitHub Desktop.
Convert bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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