Node Attachments: Display and Theme File Size

This script will allow you to display the file size of attached files uploaded to a node. It will determine and display the appropriate size of the file in bytes, kilobytes, and megabytes.

<?php
$item
= $node->filesize[0]; // change this to the correct field name
if($item['filesize'] < 1024) {
   print (
$item['filesize']);
   print (
" bytes");
}
elseif (
$item['filesize'] < 1048576) {
  
$filesize = $item['filesize'];
  
$filesize = $filesize / 1024;
  
$filesize = round($filesize, 2);
   print (
$filesize);
   print (
" kB");
}
else {
  
$filesize = $item['filesize'];
  
$filesize = $filesize / 1048576;
  
$filesize = round($filesize, 2);
   print (
$filesize);
   print (
" MB");
}
?>

Tested in Drupal 5 with the Contemplate module. Originally from http://drupal.org/node/254020#comment-915919

Drupal 5 & Drupal 6 Tip

There is a dedicated function for similar results at least in Drupal 5 and 6:

<?php
 
print format_size($file->filesize);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.