Skip to content

Instantly share code, notes, and snippets.

@grena
Last active January 25, 2019 09:34
Show Gist options
  • Save grena/5977137 to your computer and use it in GitHub Desktop.
Save grena/5977137 to your computer and use it in GitHub Desktop.
Snippet to convert bytes size to human readable size in PHP.
<?php
function filesize_formatted($path)
{
$size = filesize($path);
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment