size_format

Definition:
function size_format( $bytes, $decimals = 0 ) {}

Convert number of bytes largest unit bytes will fit into.
It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts number of bytes to human readable number by taking the number of that unit that the bytes will go into it. Supports TB value.

Parameters

  • int|string $bytes: Number of bytes. Note max integer size for integers.
  • int $decimals: Precision of number of decimal places. Deprecated.

Return values

returns:False on failure. Number string on success.

Source code

function size_format( $bytes, $decimals = 0 ) {

	$quant = array(

		// ========================= Origin ====

		'TB' => 1099511627776,  // pow( 1024, 4)

		'GB' => 1073741824,     // pow( 1024, 3)

		'MB' => 1048576,        // pow( 1024, 2)

		'kB' => 1024,           // pow( 1024, 1)

		'B ' => 1,              // pow( 1024, 0)

	);

	foreach ( $quant as $unit => $mag )

		if ( doubleval($bytes) >= $mag )

			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;



	return false;

}

2899

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: