Canman2005 Posted April 22, 2007 Share Posted April 22, 2007 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 More sharing options...
Guest prozente Posted April 22, 2007 Share Posted April 22, 2007 From the PHP Manual: echo round(1.95583, 2); // Would display 1.96 Link to comment https://forums.phpfreaks.com/topic/48085-altering-a-filesize-function/#findComment-234994 Share on other sites More sharing options...
Canman2005 Posted April 22, 2007 Author Share Posted April 22, 2007 thanks, but if I do a round then it removes the KB, MB etc off the end of the filesize Link to comment https://forums.phpfreaks.com/topic/48085-altering-a-filesize-function/#findComment-235022 Share on other sites More sharing options...
genericnumber1 Posted April 22, 2007 Share Posted April 22, 2007 thanks, but if I do a round then it removes the KB, MB etc off the end of the filesize do it in the function before the KB, MB etc is added to the string... Link to comment https://forums.phpfreaks.com/topic/48085-altering-a-filesize-function/#findComment-235023 Share on other sites More sharing options...
Navarr Posted April 22, 2007 Share Posted April 22, 2007 <?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 https://forums.phpfreaks.com/topic/48085-altering-a-filesize-function/#findComment-235025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.