Jump to content

File Size convert from bytes to MB

Recommended Posts


The db shows file sizes in bytes and was wondering if there is a way of making it human readable into MB?

Below shows where it's positioned and the hook, I just want it to show in MB.



Link to comment
Share on other sites

There is CubeCart's formatBytes(x,implode=false,dec=2) where x is the byte count, implode is true to fetch a string, array otherwise, and dec is the number of characters after the decimal point. The function will return a string that says "2.45 MB". (Or KB, MB, GB, depending how large the number is.) Or will return an array with 'size' and 'suffix' elements.


Link to comment
Share on other sites

Just call it.

The function formatBytes() is a general (globally-available) function and is located in /includes/functions.inc.php. CubeCart executes this file early in the wake-up cycle and all the functions in it are then known to CubeCart and can be used anywhere at anytime.


Link to comment
Share on other sites

Hi @bsmither,

In the template (templates/content.receipt.php) I've placed {$item.filesize} where I wanted the size of the file to show. Where and how do I call up formatBytesize to change the number and suffix?

Thanks in advance.

Link to comment
Share on other sites

The better place to make this adjustment is in the snippet shown in the first post. Add this statement after line 5:

addean2receipt_item['filesize'] = formatBytes((int)addean2receipt_item['filesize'],true);

But, now that you mention you want to do this in the template, I need to clarify one important point: Smarty, by default, does not allow the use of any userland functions, and the Smarty documentation says that only a few of PHP's functions are allowed to be used (although, I seem to be able to use any PHP function that I need). The developer must specifically enable a Smarty security setting to allow Smarty to use userland functions.

So, to do this in the template, add the following code at the top:

{function formatBytes dec=2 ext='KiB'}
{$size = $bytes / 1024}
{if $size < 1024}{$size = number_format($size, $dec)}{$ext = 'KiB'}
{elseif $size / 1024 < 1024}{$size = number_format($size / 1024, $dec)}{$ext = 'MiB'}
{elseif $size / 1024 / 1024 < 1024}{$size = number_format($size / 1024 / 1024, $dec)}{$ext = 'GiB'}

Use this to show your formatted expression:

{formatBytes bytes = $item.filesize}
Edited by bsmither
Link to comment
Share on other sites

Thanks @bsmitherI got it to work perfectly using your method; however, because file manager is for both audio and images, there are some showing on the receipt that are 90KB and some 40MB I must have to include the mimetype - audio/wav and audio/mpeg, but not sure how to implement that?

Link to comment
Share on other sites

In line 5 of the snippet, ask to get everything instead of just 'filesize' in the call to select() -- although there isn't much more to get other than the mimetype. The following is just part of line 5:


If it is more desirable to simply get just the filesize and mimetype, then instead of 'filesize', use array('filesize','mimetype').

The next thing about line 5 is that the select() function returns an array of arrays. So, specifying the element [0] is proper.

But, we may decide to incorporate everything returned in element [0] of the select() call. What must not happen is to simply make select()[0] equal to addean2receipt_item as that will replace the existing array elements of addean2receipt_item instead of what we want to do, which is merging the new array elements onto the existing array elements.

Try the following for line 5:

$addean2invoice_item = merge_array($addean2invoice_item, select()[0]);

where select()[0] is representative of the complete $GLOBALS['db'] call.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...