Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created July 21, 2012 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyrus/3157188 to your computer and use it in GitHub Desktop.
Save gyrus/3157188 to your computer and use it in GitHub Desktop.
Format the size of a file (for WordPress)
<?php
/**
* Return the formatted size of a file.
*
* @param string|int $input Either the path to a valid file, or a number in bytes
* @param string $default_output Optional. The string to output if the input can't be used (e.g. the file doesn't exist)
* @uses size_format()
* @return string The size, formatted
*/
function pilau_format_filesize( $input, $default_output = '??' ) {
$size = null;
$output = $default_output;
// Set up some common file size measurements
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if ( is_file( $input ) ) {
// Get the file size in bytes
$size = filesize( $input );
} else if ( is_numeric( $input ) ) {
$size = (int) $input;
}
if ( $size ) {
$output = size_format( $size );
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment