Need coding help - list files from set hack

Permalink
Hi all :-)
This questions goes out to the coders reading this. I've been using the "show files from set" add-on by mesuva to display a lot of files I have made available for download. I've customized it a lot and changed the way the files are downloaded, I've tried to avoid the site from beeing siteripped by adding a 1 minute pause between each download. Oh well, that part Works fine as it is now, but I'm having a bit difficulty in getting the files to display their correct size in KB, MB etc. I found a hack somewhere on the net I've edited and have gotten it to Work with the "show files from set" add-on, well, allmost Work hehe. Now please look at this page:http://files.unrealtournament.info/index.php/downloads/development-...
There you'll get a Little list of files, notice the trailing filesize text, when the filesize is 3 or more characters long is displays correct, but if it's only 1 character long it display e.g. 5KB KB grrrr..... here is the code I'm using:

if ($displaySize) 
               {
               // modified filesize hack to show Bytes,MB, GB etc. according to individual filesize
               $size = $fv->getSize();
               $TotalFileSize += $size; // count total filesize
               $units = explode(' ','KB MB GB TB PB');
               $modify = 1024;
               for ($i = 0; $size > $modify; $i++)
                  {
                  $size /= $modify;
                  }
               $endIndex = strpos($size, ".")+3;
               $title .= ' - ' . substr( $size, 0, $endIndex).' '.$units[$i];
               }

Can anyone pleasy try and spot my error?? I've been growing grey hair on this one now :-/

 
keeasti replied on at Permalink Best Answer Reply
keeasti
Looks like

$size = $fv->getSize();


may be already appending a suffix to the value. Can't say for sure but maybe you can remove your modifiers and see what it spits out.

Edit:
But you already knew that!

Try this:
if ($displaySize) 
               {
               // modified filesize hack to show Bytes,MB, GB etc. according to individual filesize
               $size = $fv->getSize();
               $size = str_replace('KB','',$size); //strips the added 'KB'
               $TotalFileSize += $size; // count total filesize
               $units = explode(' ','KB MB GB TB PB');
               $modify = 1024;
               for ($i = 0; $size >= $modify; $i++) //changed to >= for when you have a 1024KB file
                  {
                  $size /= $modify;
                  }
               $endIndex = strpos($size, ".")+3;
               $title .= ' - ' . substr( $size, 0, $endIndex).' '.$units[$i];
               }
TheDaneNet replied on at Permalink Reply
Thank you very much! That did the trick for me :-)

They all now show up flawless!

It strikes me as a very obvious solution *blushing*.
JohntheFish replied on at Permalink Reply
JohntheFish
You can see a few variations in my Image List Templates addon for List Files From Set.

http://www.concrete5.org/marketplace/addons/image-list-templates/...