Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created November 26, 2014 10:24
Show Gist options
  • Save yanknudtskov/e879d594ba787e8325ef to your computer and use it in GitHub Desktop.
Save yanknudtskov/e879d594ba787e8325ef to your computer and use it in GitHub Desktop.
Get a filesize string
<?php
function get_filesize_string($path)
{
$bytes = sprintf('%u', filesize($path));
if ($bytes > 0)
{
$unit = intval(log($bytes, 1024));
$units = array('B', 'KB', 'MB', 'GB');
if (array_key_exists($unit, $units) === true)
{
return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]);
}
}
return $bytes;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment