Customizing SMF > SMF Coding Discussion
Show Attachments as KB, MB, GB ??
Iomega0318:
--- Quote from: MrPhil on May 03, 2012, 12:39:03 PM ---In what way is it "not working correctly"? Is it changing what's displayed, but not showing the expected numbers? Is it not having any effect at all? Is it showing MB and you want MiB? In that case, use 1000000 and 1000 for MiB and kiB instead of 1048576 and 1024.
--- End quote ---
Sorry, it is working and showing MB etc, however it is not showing the correct number.
For example, I have a file that is 44254.34 kB, 442.54 MB but with this it shows as 44.2 MB.
Also in the above code I notice there is no GB conversion..
MrPhil:
You're saying that 45316444 bytes shows as 44254.34 kB and 44.2 MB? Are you sure it's not showing 43.22 MB? Remember, bytes/1024 --> kB, kB/1024 --> MB. Use 1000 if you want kiB and MiB. Where do you get 442.54 MB from?
To show GB (can your server even allow file uploads that size?):
--- Code: ---'size' => $attachment['filesize']>=1073741824
? round($attachment['filesize']/1073741824, 2) . ' GB'
: ($attachment['filesize']>=1048576
? round($attachment['filesize']/1048576, 2) . ' MB'
: ($attachment['filesize']>=1024
? round($attachment['filesize']/1024, 2) . ' kB'
: $attachment['filesize'] . ' B' )),
--- End code ---
or
--- Code: ---'size' => $attachment['filesize']>=1000000000
? round($attachment['filesize']/1000000000, 2) . ' GiB'
: ($attachment['filesize']>1000000
? round($attachment['filesize']/1000000, 2) . ' MiB'
: ($attachment['filesize']>1000
? round($attachment['filesize']/1000, 2) . ' kiB'
: $attachment['filesize'] . ' B' )),
--- End code ---
Iomega0318:
Thank you for the updated one with GB (and yes/no it can lol, kinda complicated) and after looking into it my file was only holding 8 numbers in the database so it was one short, after fixing it and it showing 9 numbers it now shows up right.. the original code does work it was just my database that was missing a number.. oops..
**EDIT**
hmm odd, if I use your code it shows everything as kB but if I use the above code it will show the correct number of MB now that I fixed the database.. any ideas why yours does not work?
MrPhil:
I think the size in the database is going to be a signed 32 bit integer (2.1 billion max). If it's unsigned, that will be 4.2 billion max. The "size" should be 10 digits, not 8 or 9, to allow the full amount. If you have 8 or 9 digits, SQL will cut off or shorten the returned value in some way, which is not desirable in this case.
I'm confused by your statements. What is now working and what isn't? Is "the above code" the latest that I gave, or something earlier? Give an example of actual size (in bytes) and what shows up displayed as the size. Remember that the size on a Windows PC will not be quite the same size as on a Linux server, if the attachment was text (uploaded in ASCII format), due to CRLF <--> newline translation. For binary files, the size should be the same.
Arantor:
--- Quote ---I think the size in the database is going to be a signed 32 bit integer (2.1 billion max). If it's unsigned, that will be 4.2 billion max. The "size" should be 10 digits, not 8 or 9, to allow the full amount. If you have 8 or 9 digits, SQL will cut off or shorten the returned value in some way, which is not desirable in this case.
--- End quote ---
Nope. MySQL uses the 'size' to figure out how big to display values for the command line client. int(8) is the same 32-bit column as int(10) just it won't display 10 columns on the command line client.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version