Software Developer

PHP Function to Convert Bytes to a Specified Unit, Whether Kilobytes, Megabytes, or Gigabytes

This function does not choose for you what units to convert to, but rather lets you specify what unit type you want to convert Bytes into.

You can choose to convert Bytes into either Kilobytes, Megabytes, or Gigabytes by passing a parameter. Here is the PHP function:

(See below for usage examples, and also 3 separate functions for conversions for those who just need one unit type.)

/**
 * Convert bytes to the unit specified by the $to parameter.
 * 
 * @param integer $bytes The filesize in Bytes.
 * @param string $to The unit type to convert to. Accepts K, M, or G for Kilobytes, Megabytes, or Gigabytes, respectively.
 * @param integer $decimal_places The number of decimal places to return.
 *
 * @return integer Returns only the number of units, not the type letter. Returns 0 if the $to unit type is out of scope.
 *
 */
function isa_convert_bytes_to_specified($bytes, $to, $decimal_places = 1) {
	$formulas = array(
		'K' => number_format($bytes / 1024, $decimal_places),
		'M' => number_format($bytes / 1048576, $decimal_places),
		'G' => number_format($bytes / 1073741824, $decimal_places)
	);
	return isset($formulas[$to]) ? $formulas[$to] : 0;
}

Usage examples

To use the function above, pass the bytes as the first parameter. For the second parameter, pass the letter for the unit type that you want to convert to. Use only one of these 3:

  • ‘K’ to convert to Kilobytes
  • ‘M’ to convert to Megabytes
  • ‘G’ to convert to Gigabytes

Here are 3 usage examples:

  1. To convert bytes to Kilobytes:

    isa_convert_bytes_to_specified($bytes, 'K');
  2. To convert bytes to Megabytes:

    isa_convert_bytes_to_specified($bytes, 'M');
  3. To convert bytes to Gigabytes:

    isa_convert_bytes_to_specified($bytes, 'G');

By default, the answer is returned with one decimal place. To change this, pass a third parameter to specify how many decimal places to return.

For example, to get Gigabytes with 3 decimal places, use this:

isa_convert_bytes_to_specified($bytes, 'G', 3);

To get Kilobytes with no decimal places, pass a zero:

isa_convert_bytes_to_specified($bytes, 'K', 0);

3 Separate Functions to Convert Bytes

Use one of the following instead if you only need to convert bytes to one specific unit type.

  1. To convert bytes to kilobytes, use this function:

    /**
     * Converts Bytes to Kilobytes
     * @param integer $bytes The filesize in Bytes.
     * @param integer $decimal_places The number of decimal places to return.
     * @return integer Returns only the number of Kilobytes, without the letters "KB"
     */
    function isa_bytes_to_kb($bytes, $decimal_places = 1 ){
    	return number_format($bytes / 1024, $decimal_places);
    }
    

    To use this function to get kilobytes, just pass the bytes:

    $kilobytes = isa_bytes_to_kb($bytes);
  2. To convert bytes to Megabytes, use this function:

    /**
     * Converts Bytes to Megabytes
     * @param integer $bytes The filesize in Bytes.
     * @param integer $decimal_places The number of decimal places to return.
     * @return integer Returns only the number of Megabytes, without the letters "MB"
     */
    function isa_bytes_to_mb($bytes, $decimal_places = 1 ){
    	return number_format($bytes / 1048576, $decimal_places);
    }
    

    To use this function to get Megabytes, just pass the bytes:

    $megabytes = isa_bytes_to_mb($bytes);
  3. To convert bytes to Gigabytes, use this function:

    /**
     * Converts Bytes to Gigabytes
     * @param integer $bytes The filesize in Bytes.
     * @param integer $decimal_places The number of decimal places to return.
     * @return integer Returns only the number of Megabytes, without the letters "GB"
     */
    function isa_bytes_to_gb($bytes, $decimal_places = 1 ){
    	return number_format($bytes / 1073741824, $decimal_places);
    }
    

    To use this function to get Gigabytes, just pass the bytes:

    $gigabytes = isa_bytes_to_gb($bytes);

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]