Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

human readable size calculation crashes in PHP 8.0 #36

Open
alex-savvii opened this issue Jun 29, 2023 · 0 comments
Open

human readable size calculation crashes in PHP 8.0 #36

alex-savvii opened this issue Jun 29, 2023 · 0 comments

Comments

@alex-savvii
Copy link

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];
}
}

Ignoring non-integer and negative values of $bytes there are two cases: $bytes == 0 and $bytes >= 1.

In case of $bytes >= 1: Everything is fine, the script runs fine the result is as expected.

In case of $bytes == 0: Things were slightly broken, if one ignored warnings, the result is "0 B", so as expected, however: In PHP 8.0 a division by zero is not a Warning anymore, but a Fatal error.

  • $log(0) = -\infty$, so $1024^{-\infty} = 0$

This is fixable by inserting something like if ($bytes < 1) { return "0 B"; } just before the first line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant