Skip to content

Instantly share code, notes, and snippets.

@driesd
Created August 29, 2013 13:16
Show Gist options
  • Save driesd/6377952 to your computer and use it in GitHub Desktop.
Save driesd/6377952 to your computer and use it in GitHub Desktop.
Formats a file size (Bytes) into something readable (KB, MB, ...)
<?php
function __format_file_fize_size( $size, $display_bytes=false ) {
if( $size < 1024 )
$filesize = $size . ' bytes';
elseif( $size >= 1024 && $size < 1048576 )
$filesize = round( $size/1024, 2 ) . ' KB';
elseif( $size >= 1048576 )
$filesize = round( $size/1048576, 2 ) . ' MB';
if( $size >= 1024 && $display_bytes )
$filesize = $filesize . ' (' . $size . ' bytes)';
return $filesize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment