Skip to Content


Return an appropriate unit for a given file size.

PHP April 13, 2021


nebula()->format_bytes($bytes, $precision)


(Required) (Integer) The size to format (in bytes)
Default: None

(Optional) (Integer) How many decimal places
Default: 2

Parameter Notes

This function uses 1,024b for a kb.

Request or provide clarification »

Additional Notes

Don’t forget that WordPress provides byte constants (KB_IN_BYTES, MB_IN_BYTES, GB_IN_BYTES) for easy readability.

Was this page helpful? Yes No

    A feedback message is required to submit this form.

    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Utilities.php on line 1006.

    No Hooks

    This function does not have any filters or actions available. Request one?
            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);
                return round($bytes, $precision) . $units[$pow];


    This function can not be short-circuited with an override filter. Request one?