Hi

print $node->field_file[0]['filesize']

prints filesize in bytes format.

Is it possible to get it in Kbytes format?

Thanks in advance.

Comments

nisaak’s picture

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!

jrglasgow’s picture

Status: Active » Postponed (maintainer needs more info)

does this take care of the problem?

shane birley’s picture

Title: Filesize format » Display and Theme File Size

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.

sin’s picture

There is a better way:

  print format_size($node->field_file[0]['filesize']);
marcushenningsen’s picture

Version: 5.x-2.01 » 6.x-1.1
Status: Postponed (maintainer needs more info) » Fixed

#4 did the trick. Thanks a lot.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.