adrian Posted April 9, 2013 Share Posted April 9, 2013 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]; } 1 Link to comment Share on other sites More sharing options...
teppo Posted April 9, 2013 Share Posted April 9, 2013 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 More sharing options...
adrian Posted April 9, 2013 Author Share Posted April 9, 2013 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. 1 Link to comment Share on other sites More sharing options...
ryan Posted April 11, 2013 Share Posted April 11, 2013 Makes sense to me. If you guys want to post a replacement function here, I'll plug it in (or pull request, whatever you prefer). 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now