Format bytes with PHP – B, KB, MB, GB, TB, PB, EB, ZB, YB converter - Comment Page: 1

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) [inttf_post_ad1] 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...

20 comments on “Format bytes with PHP – B, KB, MB, GB, TB, PB, EB, ZB, YB converter - Comment Page: 1

1 2
    1. […] "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 […]

      Reply
    2. 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];
      }

      Reply
      • Dr Garg? How strange that I should find your name on a random website. I guess we were both looking for the same kind of info. :)

        Reply
    3. not much helpful…..

      Reply
    4. Amazing!!, but in the examples, consider remove the \n concat, because the example becomes difficult to read… greetings from Argentina.

      Reply
      • Hi Juan,

        Thanks, I removed \n from examples and yes, you are right, it’s easier to read.

        Reply
    5. Thanks man, very helpful function, i implemented it in my template engine.

      Thanks again

      Reply
    6. Thanks, I owe you one.

      Reply
    7. thanx sir for hlp me

      Reply
    8. thanks, It helps a lot of !

      Reply
    9. Thanks man, very helpful function, i implemented it in my template engine.
      Thanks again

      Reply
    10. Awesome speed

      Reply
1 2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close