Created
April 26, 2022 07:24
-
-
Save stavros-melidoniotis/b409a19b3cb8b1f8a885a764de319df8 to your computer and use it in GitHub Desktop.
Return a file's size in human readable form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a file's size in human readable form | |
* | |
* @param int $bytes File's size in bytes | |
* @param int $decimals How many decimals on the returned value | |
* | |
* @return string The file's size in human readable form | |
*/ | |
function human_filesize($bytes, $decimals = 2) | |
{ | |
$sz = ['B', 'KB', 'MB', 'GB']; | |
$factor = floor((strlen($bytes) - 1) / 3); | |
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment