Skip to content

Instantly share code, notes, and snippets.

@juukie
Created December 18, 2014 12:24
Show Gist options
  • Save juukie/723dd8ff9cfa706c1d6d to your computer and use it in GitHub Desktop.
Save juukie/723dd8ff9cfa706c1d6d to your computer and use it in GitHub Desktop.
format_filesize
<?php
if ( ! function_exists('format_filesize'))
{
/**
* Formats a filesize in a human readable way.
*
* @param int $size
* @param array $sizes
* @return string
*/
function format_filesize($size, $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'])
{
// If you send 0 to the formula below, it will throw a division by zero error.
if ($size === 0) return '0 ' . $sizes[0];
return (round($size / pow(1024, ($index = floor(log($size, 1024)))), 2).' '.$sizes[$index]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment