Filesize formatting function

Discussion in 'PHP' started by pradeep, Aug 29, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Here's a function which will format the passed bytes into KB,MB,GB...appropriately.

    PHP:
    function GetSize ($sizeb) {
       
    $sizekb = $sizeb / 1024;
       
    $sizemb = $sizekb / 1024;
       
    $sizegb = $sizemb / 1024;
       
    $sizetb = $sizegb / 1024;
       
    $sizepb = $sizetb / 1024;
       if (
    $sizeb > 1) {$size = round($sizeb,2) . "b";}
       if (
    $sizekb > 1) {$size = round($sizekb,2) . "kb";}
       if (
    $sizemb > 1) {$size = round($sizemb,2) . "mb";}
       if (
    $sizegb > 1) {$size = round($sizegb,2) . "gb";}
       if (
    $sizetb > 1) {$size = round($sizetb,2) . "tb";}
       if (
    $sizepb > 1) {$size = round($sizepb,2) . "pb";}
       return 
    $size;
     }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice