Usage
PHP
nebula()->format_bytes($bytes, $precision)
Parameters
$bytes
(Required) (Integer) The size to format (in bytes)
Default: None
$precision
(Optional) (Integer) How many decimal places
Default: 2
Parameter Notes
This function uses 1,024b for a kb.
Additional Notes
Don’t forget that WordPress provides byte constants (KB_IN_BYTES, MB_IN_BYTES, GB_IN_BYTES) for easy readability.
Source File
Located in /libs/Utilities/Utilities.php on line 1268.
No Hooks
This function does not have any filters or actions available. Request one?
PHP
public function format_bytes($bytes, $precision=1){
$units = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
$bytes = max($bytes, 0);
$base = ( $bytes )? log($bytes) : 0;
$pow = floor($base/log(1024));
$pow = min($pow, count($units)-1);
$bytes = $bytes/pow(1024, $pow);
$decimals = ( $pow >= 2 )? $precision : 0; //Only show the decimal places for files larger than 1mb
return round($bytes, $decimals) . $units[$pow];
}
Override
This function can not be short-circuited with an override filter. Request one?