Calculate bytes in GB,MB format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheswaran
    New Member
    • Mar 2007
    • 190

    Calculate bytes in GB,MB format

    Hi,

    I calculate a folder size.But it is return in bytes format. I want to this in gb or mb size according to the size.

    Is there any function.?
  • MarkSponge
    New Member
    • Apr 2008
    • 6

    #2
    Could you not simply divide the number of bytes returned by 1048576 to get the megabytes or by 1073741824 to get the gigabytes?

    Then after you calculate the Mb and the Gb in the function do somethinig like:
    Code:
    if($gigabytes > 1){
        return $gigabytes." Gb";
    }else if($megabytes > 1){
        return $megabytes." Mb";
    }else{
        return $bytes." bytes";
    }
    That will return either Gb, Mb or just bytes depending on the size.

    Is this what you meant??

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      I couldn't find a standard function to do this, but I've used this in some of my projects:
      [code=php]
      function bytesConvert($b ytes)
      {
      $ext = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
      $unitCount = 0;
      for(; $bytes > 1024; $unitCount++) $bytes /= 1024;
      return $bytes ." ". $ext[$unitCount];
      }
      [/code]

      Comment

      Working...