Community Documentation

Node Attachments: Display and Theme File Size

Last updated September 6, 2009. Created by Shane Birley on July 10, 2008.
Edited by bekasu, ronald_istos, jhodgdon. Log in to edit this page.

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);
?>

Comments

Which part should be changed

Which part should be changed in the following sentence?

"$item = $node->filesize[0]; // change this to the correct field name"

I have just recently started using Drupal and am still a total novice, but I've got a file field in a view and would like another field to display the filesize of the file field.

Thanks,
Jethro

About this page

Drupal version
Drupal 5.x
Audience
Developers and coders, Themers

Theming Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here