File Size Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prince of Code

    File Size Conversion

    Can anybody help me out by writing a function for the given problem

    Write a function in PHP that converts given number of bytes into
    Readabel format like 99.92 KB for 102323 bytes and
    3 MB for 3072 bytes and so on. Let your function be
    sophasticated,p ortable,easy readable.

    Thankzz in advance
    Napoleon

  • Peter Fox

    #2
    Re: File Size Conversion

    Following on from Prince of Code's message. . .
    You seem to have posted to comp.lang.php by mistake
    I think you meant comp.rtfm or possibly comp.domy.cours ework

    [color=blue]
    >Can anybody help me out by writing a function for the given problem
    >
    >Write a function in PHP that converts given number of bytes into
    >Readabel format like 99.92 KB for 102323 bytes and
    >3 MB for 3072 bytes and so on. Let your function be
    >sophasticated, portable,easy readable.
    >
    >Thankzz in advance
    >Napoleon
    >[/color]

    --
    PETER FOX Not the same since the exam marking business failed
    peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
    2 Tees Close, Witham, Essex.
    Gravity beer in Essex <http://www.eminent.dem on.co.uk>

    Comment

    • Prince of Code

      #3
      Re: File Size Conversion

      I dont understand anything

      Peter Pls give something if relavant and usefull

      Thankzz
      Prince of Code

      Comment

      • Philip Ronan

        #4
        Re: File Size Conversion

        "Prince of Code" wrote:
        [color=blue]
        > I dont understand anything[/color]

        No shit.

        One of these days I'm going to start filtering out everything from Google
        Groups. :-(

        --
        phil [dot] ronan @ virgin [dot] net


        Comment

        • Richard Morton

          #5
          Re: File Size Conversion

          Hi Prince of Code,

          What you are asking is quite simple. But with a name like Prince Of Code
          it looks like you're too lazy to do the ground work and learn the basics
          of the language.

          I am sure you'll appreciate that we have our own projects to complete
          and so can't hand hold people through every step of the learning process.

          You might want to look at a simple function like this though (untested)

          function readableFilesiz e($bytes){
          switch(TRUE){
          case ($bytes<1024)
          return ($bytes . "bytes");
          break;
          case ($bytes>1023)
          return ($bytes/1024 . "KB");
          break;
          case ($bytes>104858)
          return ($bytes/1000000 . "MB");
          break;
          }
          }


          now, I'm not doing the rest for you, you need to look for someway to
          round the answers and add it into the returns

          www.php.net and look for rounding functions.

          HTH - but please do some work yourself before posting!

          Rich


          Prince of Code wrote:[color=blue]
          > Can anybody help me out by writing a function for the given problem
          >
          > Write a function in PHP that converts given number of bytes into
          > Readabel format like 99.92 KB for 102323 bytes and
          > 3 MB for 3072 bytes and so on. Let your function be
          > sophasticated,p ortable,easy readable.
          >
          > Thankzz in advance
          > Napoleon
          >[/color]

          Comment

          • Java Boy

            #6
            Re: File Size Conversion

            1 KB == 1024 bytes
            rest you can figure out yourself

            --
            Geeks Home





            "Prince of Code" <princeofcode@g mail.com> wrote in message
            news:1134977390 .809166.140530@ o13g2000cwo.goo glegroups.com.. .[color=blue]
            > Can anybody help me out by writing a function for the given problem
            >
            > Write a function in PHP that converts given number of bytes into
            > Readabel format like 99.92 KB for 102323 bytes and
            > 3 MB for 3072 bytes and so on. Let your function be
            > sophasticated,p ortable,easy readable.
            >
            > Thankzz in advance
            > Napoleon
            >[/color]


            Comment

            • James Wheaton

              #7
              Re: File Size Conversion

              Here's a function a wrote for a previous program:

              /*************** *************
              * File size unit converter *
              *************** *************/
              function fsize_unit_conv ert($bytes)
              {
              $units = array('b', 'kb', 'mb', 'gb');
              $converted = $bytes . ' ' . $units[0];
              for ($i = 0; $i < count($units); $i++)
              {
              if (($bytes/pow(1024, $i)) >= 1)
              {$converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];}
              }
              return $converted;
              }

              Java Boy wrote:
              [color=blue]
              > 1 KB == 1024 bytes
              > rest you can figure out yourself
              >
              > --
              > Geeks Home
              > www.fahimzahid.com
              >
              >
              >
              >
              > "Prince of Code" <princeofcode@g mail.com> wrote in message
              > news:1134977390 .809166.140530@ o13g2000cwo.goo glegroups.com.. .[color=green]
              >> Can anybody help me out by writing a function for the given problem
              >>
              >> Write a function in PHP that converts given number of bytes into
              >> Readabel format like 99.92 KB for 102323 bytes and
              >> 3 MB for 3072 bytes and so on. Let your function be
              >> sophasticated,p ortable,easy readable.
              >>
              >> Thankzz in advance
              >> Napoleon
              >>[/color][/color]

              --

              Comment

              • Jaak

                #8
                Re: File Size Conversion

                This is what I use.
                Maybe not so complicated, but more easy readable. It gives 0.6Mb instead of
                600 kb etc Off course you can expand this with Gb etc...

                if ($filesize < 500)
                {
                $filesize = "$grootte b";
                }
                elseif ($filesize <((1024 * 1024)/2))
                {
                $filesize = round ($filesize / 1024,1);
                $filesize = "$filesize Kb";
                }
                else
                {
                $filesize = round (($filesize / 1024 / 1024),2);
                $filesize = "$filesize Mb";
                }


                "James Wheaton" <apoc_gfx@yahoo .com> schreef in bericht
                news:cZ_uf.6850 49$_o.478491@at tbi_s71...[color=blue]
                > Here's a function a wrote for a previous program:
                >
                > /*************** *************
                > * File size unit converter *
                > *************** *************/
                > function fsize_unit_conv ert($bytes)
                > {
                > $units = array('b', 'kb', 'mb', 'gb');
                > $converted = $bytes . ' ' . $units[0];
                > for ($i = 0; $i < count($units); $i++)
                > {
                > if (($bytes/pow(1024, $i)) >= 1)
                > {$converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];}
                > }
                > return $converted;
                > }
                >
                > Java Boy wrote:
                >[color=green]
                >> 1 KB == 1024 bytes
                >> rest you can figure out yourself
                >>
                >> --
                >> Geeks Home
                >> www.fahimzahid.com
                >>
                >>
                >>
                >>
                >> "Prince of Code" <princeofcode@g mail.com> wrote in message
                >> news:1134977390 .809166.140530@ o13g2000cwo.goo glegroups.com.. .[color=darkred]
                >>> Can anybody help me out by writing a function for the given problem
                >>>
                >>> Write a function in PHP that converts given number of bytes into
                >>> Readabel format like 99.92 KB for 102323 bytes and
                >>> 3 MB for 3072 bytes and so on. Let your function be
                >>> sophasticated,p ortable,easy readable.
                >>>
                >>> Thankzz in advance
                >>> Napoleon
                >>>[/color][/color]
                >
                > --
                > http://www.uranther.com[/color]


                Comment

                Working...