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
https://forums.phpfreaks.com/topic/48085-altering-a-filesize-function/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.