Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts

    Question converting kilobytes to megabytes

    hi i wondering if there is a way to make some text on the screen change from kilobytes to megabytes. lets say on a page it says 99857 KB is there some kind of replace variable or anything that would automatically convert that number to like 99mb instead automatically? this seems like it would be something complicated to me. to clarify im talking about something that would automatically change random instances of different amounts of kilobytes. lets say we have a list of

    12323kb
    32463kb
    34353kb
    34256kb
    and convert those to megabytes.
    Last edited by cosmicsea; 01-23-2010 at 11:11 PM.

  • #2
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts
    i use a code like this
    PHP Code:
    <?php print($data['size']); ?>
    and it will automatically show the size of whatever its connected to in kilobytes.

  • #3
    Regular Coder
    Join Date
    Feb 2008
    Posts
    119
    Thanks
    23
    Thanked 2 Times in 2 Posts
    I use this to determine the size of files that have been uploaded to my site.

    PHP Code:
      $filesize filesize($filename);
     
    $filesize = ($filesize 1048576);
       if (
    $filesize 1)  {
        
    $filesize round($filesize*1024,0);
        
    $check1 "KB";
      }  else    {
        
    $filesize round($filesize,2);
        
    $fsizetxt "".$fsize." MB";
        
    $check1 "MB";
    }
    echo 
    round($filesize,2).$check1

  • Users who have thanked Jedi Knight for this post:

    cosmicsea (01-23-2010)

  • #4
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Jedi Knight View Post
    I use this to determine the size of files that have been uploaded to my site.

    PHP Code:
      $filesize filesize($filename);
     
    $filesize = ($filesize 1048576);
       if (
    $filesize 1)  {
        
    $filesize round($filesize*1024,0);
        
    $check1 "KB";
      }  else    {
        
    $filesize round($filesize,2);
        
    $fsizetxt "".$fsize." MB";
        
    $check1 "MB";
    }
    echo 
    round($filesize,2).$check1
    yeah but it there a way to make it grab the KB info from the code i use and replace it with megabytes?

  • #5
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    well if $data['size'] contains the size couldn't you just do something like this.
    PHP Code:
    $size $data['size'];
    $size preg_replace("#[\D]#i"''$size);// strip the string down to just numbers
    $size round($size/1024);// convert and round to the nearest MB
    echo $size.'MB'
    Last edited by ninnypants; 01-23-2010 at 11:06 PM.

  • Users who have thanked ninnypants for this post:

    cosmicsea (01-23-2010)

  • #6
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ninnypants View Post
    well if $data['size'] contains the size couldn't you just do something like this.
    PHP Code:
    $size $data['size'];
    $size preg_replace("#[\D]#i"''$size);// strip the string down to just numbers
    $size round($size/1024);// convert and round to the nearest MB
    echo $size.'MB'
    yes that does work, thanks alot!

  • #7
    Regular Coder
    Join Date
    Feb 2008
    Posts
    119
    Thanks
    23
    Thanked 2 Times in 2 Posts
    That should work if you are only dealing with MB.
    The code I have will display as KB for files under 1 MB, and then MB for all files over 1 MB.
    I guess it depends on what it is used for.

    Try this one.

    PHP Code:
      $filesize $data['size'];
     
    $filesize = ($filesize 1048576);
       if (
    $filesize 1)  {
        
    $filesize round($filesize*1024,0);
        
    $check1 "KB";
      }  else    {
        
    $filesize round($filesize,2);
        
    $fsizetxt "".$fsize." MB";
        
    $check1 "MB";
    }
    echo 
    round($filesize,2).$check1
    Sorry, I didn't see your reply.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •