Format a Filesize from the bytes

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

Added by JC on 14th January 2008

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment