show file size - #26
Conversation
| $filesize = filesize($logFileName); | ||
| $sz = 'BKMGTP'; | ||
| $factor = floor((strlen($filesize) - 1) / 3); | ||
| $filesize = sprintf("%.2f", $filesize / pow(1024, $factor)) . @$sz[$factor]; |
There was a problem hiding this comment.
This is some pretty cool and scary code to generate the file size in appropriate units! Did you make this up or is it pretty battle tested?
There was a problem hiding this comment.
Okay, seems like there are a plethora of examples similar to this out on the web and that this should work most of the time. I suppose since this isn't run-time critical code I should be less concerned about edge cases, but for example - what happens if filesize is empty? Then factor is negative one, and all hell breaks loose? Using sz as a string and then getting an index of it is clever but scary for those of us who remember pointer arithmetic and out-of-bounds disasters. So ... I think this is okay except for a test for length of filesize > 0 and sz as an array instead of string.
|
@twomice I'm looking at you and what did I say about scope creep? |
|
@adixon Er, well ... [stares at ceiling awkwardly] |
|
@adixon I've found this code in the web, and I thought it was interesting to apply a different approach on how filesize is calculated, just to have some variety on the classic approach to get the file size. I'm using it already in prod as a patch, and it works fine. If you have doubts about it, I can rework it in a classic approach. tx! |
Co-authored-by: demeritcowboy <demeritcowboy@hotmail.com>
|
My two cents: a) I think it's better to add the filesize after the file name at the top of the page (instead of displaying it on the download button). b) human readable filesize The pretty cool and scary code in the PR is mentioned in the "User Contributed Notes" in the php.net documentation of I like one I found on stackoverflow: /**
* Converts a long string of bytes into a readable format e.g KB, MB, GB, TB, YB
*
* @param {Int} num The number of bytes.
*/
function readableBytes($bytes) {
$i = floor(log($bytes) / log(1024));
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
return sprintf('%.02F', $bytes / pow(1024, $i)) * 1 . ' ' . $sizes[$i];
}I think we should use a function for this. |
|
Digging a little deeper into PHP's
But I could offer a PR like proposed above - should I? |
|
PR welcome! |
|
I created a new PR #40 |

No description provided.