Display and Theme File Size
miles28 - May 2, 2008 - 18:29
| Project: | Content Templates (Contemplate) |
| Version: | 5.x-2.01 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active (needs more info) |
Jump to:
Description
Hi
<?php
print $node->field_file[0]['filesize']
?>prints filesize in bytes format.
Is it possible to get it in Kbytes format?
Thanks in advance.

#1
I had a similar issue, but I added some php code that did the calculation for me. I've posted the code below.
<div class="field field-type-file field-field-file"><p>
<h5 class="field-label">File</h5>
<div class="field-items">
<table>
<tr>
<?php foreach ((array)$node->field_file as $item) { ?>
<tr>
<td>
<div class="field-item">
<?php print $item['view'] ?>
</div>
</td>
<td>
<div class="field-item">
<?php
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");
}
?>
</div>
</td>
</tr>
<?php } ?>
</table>
</div>
</p>
</div>
If anyone knows of a better way to do this, I would love to hear it!
#2
does this take care of the problem?
#3
Almost. The script isn't recognizing the bytes to kilobytes. Here is a revised version of the script.
<?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");
}
?>
This has been tested and works in Drupal 5. It should work in Drupal 6. Thanks to Speedtech.it for helping fixing this up.
#4
There is a better way:
<?phpprint format_size($node->field_file[0]['filesize']);
?>