Format bytes with PHP – B, KB, MB, GB, TB, PB, EB, ZB, YB converter

php-logo

Simple PHP function that formats the bytes to the desired form. Possible unit options are:

  • Byte (B)
  • Kilobyte (KB)
  • Megabyte (MB)
  • Gigabyte (GB)
  • Terabyte (TB)
  • Petabyte (PB)
  • Exabyte (EB)
  • Zettabyte (ZB)
  • Yottabyte (YB)

Function takes three parameter: (bytes mandatory, unit optional, decimals optional)

PHP byteFormat function for formatting bytes

<?php
 
  function byteFormat($bytes, $unit = "", $decimals = 2) {
	$units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 
			'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);
 
	$value = 0;
	if ($bytes > 0) {
		// Generate automatic prefix by bytes 
		// If wrong prefix given
		if (!array_key_exists($unit, $units)) {
			$pow = floor(log($bytes)/log(1024));
			$unit = array_search($pow, $units);
		}
 
		// Calculate byte value by prefix
		$value = ($bytes/pow(1024,floor($units[$unit])));
	}
 
	// If decimals is not numeric or decimals is less than 0 
	// then set default value
	if (!is_numeric($decimals) || $decimals < 0) {
		$decimals = 2;
	}
 
	// Format output
	return sprintf('%.' . $decimals . 'f '.$unit, $value);
  }
 
?>

Example usage

echo byteFormat(4096, "B") ."\n";
echo byteFormat(8, "B", 2) . "\n";
echo byteFormat(1, "KB", 5) . "\n";
echo byteFormat(1073741824, "B", 0) . "\n";
echo byteFormat(1073741824, "KB", 0) . "\n";
echo byteFormat(1073741824, "MB") . "\n";
echo byteFormat(1073741824) . "\n";
echo byteFormat(1073741824, "TB", 10) . "\n";
echo byteFormat(1099511627776, "PB", 6) . "\n";

Prints

4096.00 B
8.00 B
0.00098 KB
1073741824 B
1048576 KB
1024.00 MB
1.00 GB
0.0009765625 TB
0.000977 PB

Related posts:

  1. PHP mb_ucfirst Make a String’s First Character Uppercase-Multibyte (UTF-8) Function PHP’s ucfirst function is very usefull when you want to change words first letters to uppercase and other letters to...
  2. PHP: Loop through dates (from date to date) with strtotime() function This is very easy way loop through dates (from date to date) with PHP strtotime() function. This example only echo...
  3. PHP __DIR__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__, __NAMESPACE__ PHP has large number of predefined constants. This HOWTO will present the seven most important, most practical and most useful...
  4. PHP Class for Coloring PHP Command Line (CLI) Scripts Output – PHP Output Colorizing Using Bash Shell Colors PHP Command Line Interface (CLI) has not built-in coloring for script output, like example Perl language has (perldoc.perl.org/Term/ANSIColor.html). So I...
  5. PHP: Calculate Real Differences Between Two Dates or Timestamps I was using simple function to calculate difference between two dates and timestamps until I noticed, it’s not working correctly...

About the Author

Hi, I'm JR and the Founder of if not true then false. I am a software developer, and I have over ten years experience in programming. I'm big fan of Linux and Open Source. And normally I use just Linux environments. - You can follow me on Twitter at @zuissi