Subscribe to RSS Feed

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

Bookmark & Share

Wondering what to do next?

Grab My Feed

Subscribe to RSS Feed

Catch Me On Twitter

Catch Me On Twitter!

Sponsors


2 Responses to “ Format bytes with PHP – B, KB, MB, GB, TB, PB, EB, ZB, YB converter ”

  1. Egonomik » PHP – Byte dönüştürme fonksiyonu (Convert bytes)
    January 5, 2010 at 11:11 am

    [...] "n"; echo byteFormat(1099511627776, "PB", 6) . "n";Kaynak: if-not-true-then-false.com Düzenleme için Umut‘a teşekkürler. Benzer YazılarPHP – Resim üzerine yazı yazma [...]

  2. Oliver Nassar
    January 10, 2010 at 7:05 am

    Raaaddddd!
    Thanks dude. Saved me a bunch of time.

    I used this for formating bytes, btw:

    /**
    * filesize function.
    *
    * @access public
    * @static
    * @param mixed $size
    * @param int $round. (default: 0)
    * @return void
    */
    // http://www.veign.com/code-view.php?type=web&codeid=74
    public static function filesize($size, $round = 0) {
    $sizes = array(‘b’, ‘kb’, ‘meg’, ‘gb’, ‘tb’, ‘pb’, ‘eb’, ‘zb’, ‘yb’);
    for ($i=0; $size > 1024 && isset($sizes[$i+1]); $i++) $size /= 1024;
    return round($size, $round).$sizes[$i];
    }

Leave a Reply

Recent Comments