Snippets v2

Languages
My Snippets

Format a Filesize from the bytes Login or Register to Bookmark this snippet

Description PHP

Returns a formatted string like 2.5kb from an integer of bytes. Will only go as far as Mb, but you could modify it to accomodate Gb easily.

The Code Download
  1. <?php
  2. function GetSize($bytes) {
  3.     if ($bytes > 1024) {
  4.         $size_kb = $bytes / 1024;
  5.         if ($size_kb > 1024) {
  6.             $size_mb = $size_kb / 1024;
  7.             $sizer = number_format($size_mb,2,".",",") . " Mb";
  8.         } else {
  9.             $sizer = number_format($size_kb,0,".",",") . " Kb";
  10.         }
  11.     } else {
  12.         $sizer = $bytes . " bytes";
  13.     }
  14.     return $sizer;
  15. }
  16. ?>
Credits Contact JC

Added by JC on 13th January, 2008

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment