Skip to Content
Menu

format_bytes()

Return an appropriate unit for a given file size.

PHP April 13, 2021

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.

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?
    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);
    
                return round($bytes, $precision) . $units[$pow];
            }
    

    Override

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