problem with calculating

Discussion in 'PHP' started by izlik, May 19, 2007.

  1. #1
    hello, i have a premade Image hosting script, and i have a little problem with it. on the frontpage it displayes the maximum amount you are allowed to upload per upload wich is 2MB but it's displayed in kilobytes. i wonder if someone can help me here cause i want it to be displayed in MB's

    on the frontpage file this code is displaed.

    Main.php
    $divd = 1024;
    $isize = $max_size / $divd;
    $isize2 = round($isize);
    
    Select an image file to upload<br>
    Max file size is set at <?echo $isize2;?> KB<br><br>
    PHP:
    when it's ecoing out the ammount it's displayed as (2070 KB) and i want it as (2 MB)

    and in my config file i have this

    config.php
    $max_size = 2120000;          				//max. allowed size in bytes (500 kilobytes == 512000 bytes)
    
    PHP:
    anyone know how t omake it be displayed in correct?
     
    izlik, May 19, 2007 IP
  2. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This generalizes the problem a little:

    <?php
    // useful function
    function bytesinunits ($bytes, $unit="KB") {
      switch ($unit) {
        case "KB":
          $div = 1024;
          break;
        case "MB":
          $div = 1024*1024;
          break;
        case "GB":
          $div = 1024*1024*1024;
          break;
        default:
          $div = 1024;
          $unit = "KB";
      }
      $size = round ($bytes / $div);
      return "$size $unit";
    }
    
    // test the function
    $max_size = 2*1024*1024;
    echo bytesinunits ($max_size, "MB") . "<br/>";
    ?>
    
    PHP:
     
    lemaitre, May 19, 2007 IP
  3. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #3
    you can simply change
    $divd=1024*1024;
    and Max file size is set at <?echo $isize2;?> MB<br><br>

    then it will show the result in MB.

    lemaitre's code is ok and good.but for simplicity you can just do these changes.
     
    coderbari, May 19, 2007 IP
  4. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #4
    how do you mena coderbari? if i just change KB to MB it will say 2070MB...

    laso, if i used lemaitre's script it ignores the config files and allowes people to upload files bigger then 2MB :(
     
    izlik, May 19, 2007 IP
  5. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That was just an example, if you take out the line that sets max_size it will use the value from your config file just as you intended.
     
    lemaitre, May 19, 2007 IP
  6. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    First, change your max_size variable to the proper value:

    
    $max_size = 2097152;                  //max. allowed size in bytes (500 kilobytes == 512000 bytes) 
    
    PHP:
    2 MB = 2 x 1024 KB x 1024 bytes = 2097152 bytes

    Then, change your first code to look in the end like this:

    
    $isize = floor($max_size /1048576);
    
    Select an image file to upload<br>
    Max file size is set at <?echo $isize;?> MB<br><br>
    
    PHP:
    This code will show 2 MB as you wanted. But, if you set max_size a bit higher than the 2 MB value, it will still show 2 MB because floor function does just that, it removes the information after the "." and returns only 2.

    If you want to see something like "2.05 MB" then you can use this code:

    
    $isize = round($max_size /1048576,2);
    
    Select an image file to upload<br>
    Max file size is set at <?echo $isize;?> MB<br><br>
    
    PHP:
    In this second example we're using round instead of floor because we're actually interested in the value after the ".", and we use the second parameter (in the example it has the value 2) in order to specify to PHP that we want up to 2 numbers after ".".

    The value 1048576 is 1 MB in bytes ( 1 x 1024 KB x 1024 Bytes)
     
    mariush, May 19, 2007 IP