Skip to content

Instantly share code, notes, and snippets.

@okjake
Created December 21, 2011 17:34
Show Gist options
  • Select an option

  • Save okjake/1506897 to your computer and use it in GitHub Desktop.

Select an option

Save okjake/1506897 to your computer and use it in GitHub Desktop.
Printing pretty filesizes in a Drupal theme
/*
* Takes a filesize integer and returns a decently formatted string
* This function needs to go into template.php, don't forget to change the theme name below
* Call it like this:
* - print mytheme_format_filesize($node->field_downloads[0]['filesize']
*/
function mytheme_format_filesize($filesize) {
if ($filesize < 1024) {
return $filesize . ' bytes';
}
elseif ($filesize < 1048576) {
return round($filesize / 1024) . ' kB';
}
else {
return round($filesize / 1048576) . ' MB';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment