Skip to content

Instantly share code, notes, and snippets.

@stavros-melidoniotis
Created April 26, 2022 07:24
Show Gist options
  • Save stavros-melidoniotis/b409a19b3cb8b1f8a885a764de319df8 to your computer and use it in GitHub Desktop.
Save stavros-melidoniotis/b409a19b3cb8b1f8a885a764de319df8 to your computer and use it in GitHub Desktop.
Return a file's size in human readable form
<?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