Created
July 21, 2012 21:05
-
-
Save gyrus/3157188 to your computer and use it in GitHub Desktop.
Format the size of a file (for WordPress)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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