Jump to content

Changes to $file->filesizeStr


adrian
 Share

Recommended Posts

Just wondering about the output from $file->filesizeStr

I have only used it once so far, but was surprised to see it return 10,375kB. I would rather have seen 10.4mB

Not sure what others think, but I have always used this function and been happy with the output. Thanks for any feedback.

function human_file_size($size) {
   if($size == 0) {
       return("0 Bytes");
   }
   $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
   return round($size/pow(1024, ($i = floor(log($size, 1024)))), 1) . $filesizename[$i];
}
 
  • Like 1
Link to comment
Share on other sites

Not sure what others think, but I have always used this function and been happy with the output. Thanks for any feedback.

function human_file_size($size) {
   if($size == 0) {
       return("0 Bytes");
   }
   $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
   return round($size/pow(1024, ($i = floor(log($size, 1024)))), 1) . $filesizename[$i];
}
 

I agree that it's a bit strange for filesizeStr() to always return value in kB. Haven't used it though, so it didn't bother me before :)

Generally speaking your method seems fine, there are just couple of minor things that caught my eye:

  • Those  's could cause a problem if used outside of HTML context. They're also not being used consistently ("0 Bytes").
  • It seems a bit strange that it returns "Bytes" instead of "B". All other formats are abbreviations, why not this one?
  • Abbreviation for kilobyte should IMHO be kB, not KB.

I've been using a slightly modified version of what you can find here for file-related stuff and that seems to work nicely too.

Link to comment
Share on other sites

All good points teppo :)

It's one of those canned functions I found somewhere a long time ago and haven't really thought too much about it. Its formatting could certainly be improved. Mostly I just wanted to get the point across that I think filesizeStr should do some rounding and unit changes when appropriate.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...