Pete Jones Posted June 1, 2016 Share Posted June 1, 2016 Does anyone have a reference to what you can add as formatting parameters? I assume you can return MB, KB, GB without having to divide them manually? Link to comment Share on other sites More sharing options...
DaveP Posted June 1, 2016 Share Posted June 1, 2016 Don't think it takes parameters. Seems to use this function - wireBytesStr() - https://github.com/ryancramerdesign/ProcessWire/blob/7e8c7c6836282b6b64de81263f5aaa8112fd51ae/wire/core/Functions.php#L1290 < 1024b = bytes, >1024 = kb Link to comment Share on other sites More sharing options...
Pete Jones Posted June 1, 2016 Author Share Posted June 1, 2016 This should be extended to something like: function formatBytes($size, $precision = 2) { $base = log($size, 1024); $suffixes = array('', 'K', 'M', 'G', 'T'); return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)]; } echo formatBytes(24962496); // 23.81M echo formatBytes(24962496, 0); // 24M echo formatBytes(24962496, 4); // 23.8061M *Taken from http://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes 1 Link to comment Share on other sites More sharing options...
DaveP Posted June 1, 2016 Share Posted June 1, 2016 ...or automatically like wireRelativeTimeStr() Link to comment Share on other sites More sharing options...
kongondo Posted June 1, 2016 Share Posted June 1, 2016 Meanwhile, you can use filesize and format its output however you wish... 1 Link to comment Share on other sites More sharing options...
adrian Posted June 9, 2016 Share Posted June 9, 2016 This is the one I have always liked: http://jeffreysambells.com/2012/10/25/human-readable-filesize-php But I would also vote for an option to automatically calculate the number of decimal places / significant figures. 2 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