Jump to content

Altering a filesize function


Canman2005

Recommended Posts

Hi all

 

I have the following function

 

<?php
function size_hum_read($size){
  $i=0;
  $iec = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  while (($size/1024)>1) {
   $size=$size/1024;
   $i++;
  }
  return substr($size,0,strpos($size,'.')+4).$iec[$i];
}
?>

 

Basically it reads a file on the server and returns the size of the file.

 

I pull the function using

 

<?php print size_hum_read(filesize('image.jpg')); ?>

 

It returns something like

 

23.213KB

 

The problem im coming across is that the last part of the filesize is always 3 digits long, for example

 

234.432KB

2.328MB

 

Would anyone know how I could round up the last 3 digits into 2 digits?

 

Basically

 

234.432KB

would become

234.43KB

 

and

 

2.328MB

would become

2.33MB

 

Can this be done?

 

Thanks

 

Ed

Link to comment
Share on other sites

<?php
     function size_hum_read($size) {
          $i = 0;
          $iec = array("B","KB","MB","GB","TB","PB","EB","ZB","YB");
          while (($size/1024)>1) {
               $size = $size/1024;
               $i++;
          }
          return round($size,2).$iec[$i];
     }
?>

 

Should do what you want.  It takes prozente's idea, and incorporates it properly into the script.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.