Back to Top

Properly Format your Filesize in WordPress

Previous Post:

Properly Format your Filesize in WordPress

When you display attachments within a post/page and want to show the attachments’ sizes next to their filenames, you would probably do something like this:

  1. // size calculation
  2. $docsize = @filesize($file_path);
  3. // show the attachment
  4. echo $filename . ' — ' . $docsize;
// size calculation
$docsize = @filesize($file_path);
// show the attachment
echo $filename . ' — ' . $docsize;

and the output would be something like this:

attachment.zip — 13143

Looking nice but you can make it even nicer if you format the $docsize variable, i.e. show the size unit next to it (e.g. kB, MB, etc.) WordPress fortunately has a built-in function that allows you to format file sizes easily. The magic function is size_format(), located in wp-includes/functions.php:

  1. function size_format( $bytes, $decimals = 0 ) {
  2.     $quant = array(
  3.         // ========================= Origin ====
  4.         'TB' => 1099511627776,  // pow( 1024, 4)
  5.         'GB' => 1073741824,     // pow( 1024, 3)
  6.         'MB' => 1048576,        // pow( 1024, 2)
  7.         'kB' => 1024,           // pow( 1024, 1)
  8.         'B ' => 1,              // pow( 1024, 0)
  9.     );
  10.     foreach ( $quant as $unit => $mag )
  11.         if ( doubleval($bytes) >= $mag )
  12.             return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
  13.  
  14.     return false;
  15. }
function size_format( $bytes, $decimals = 0 ) {
	$quant = array(
		// ========================= Origin ====
		'TB' => 1099511627776,  // pow( 1024, 4)
		'GB' => 1073741824,     // pow( 1024, 3)
		'MB' => 1048576,        // pow( 1024, 2)
		'kB' => 1024,           // pow( 1024, 1)
		'B ' => 1,              // pow( 1024, 0)
	);
	foreach ( $quant as $unit => $mag )
		if ( doubleval($bytes) >= $mag )
			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;

	return false;
}

Now when you output your attachments, use this instead:

  1. // size calculation
  2. $docsize = @filesize($file_path);
  3. if (FALSE === $docsize)
  4.     $docsize = 0;
  5. else
  6.     $docsize = size_format($docsize);
  7. // show the attachment
  8. echo $filename . ' — ' . $docsize;
// size calculation
$docsize = @filesize($file_path);
if (FALSE === $docsize)
	$docsize = 0;
else
	$docsize = size_format($docsize);
// show the attachment
echo $filename . ' — ' . $docsize;

and your output will be:

attachment.zip — 13 kB

Looking better now, right? What about the decimal? Just change line 6 of the above snippet to this:

  1. $docsize = size_format($docsize, 2);
$docsize = size_format($docsize, 2);

The number we have just added will determine how many numbers will appear after the dot. If you change that number to 3, your output will now be:

attachment.zip — 12.835 kB

Sweet, eh? ;)

WPtouch Pro - The best mobile plugin for WordPress!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: