Cpup Posted June 14, 2021 Share Posted June 14, 2021 Hi, 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. Thanks Link to comment Share on other sites More sharing options...
bsmither Posted June 14, 2021 Share Posted June 14, 2021 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 More sharing options...
Cpup Posted June 15, 2021 Author Share Posted June 15, 2021 Thanks @bsmither Where do I find it and how do I implement it? Link to comment Share on other sites More sharing options...
bsmither Posted June 15, 2021 Share Posted June 15, 2021 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 More sharing options...
Cpup Posted June 16, 2021 Author Share Posted June 16, 2021 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 More sharing options...
bsmither Posted June 16, 2021 Share Posted June 16, 2021 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'} {/if} {$size} {$ext} {/function} Use this to show your formatted expression: {formatBytes bytes = $item.filesize} Link to comment Share on other sites More sharing options...
Cpup Posted June 17, 2021 Author Share Posted June 17, 2021 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 More sharing options...
bsmither Posted June 17, 2021 Share Posted June 17, 2021 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: ...select('CubeCart_filemanager',false,... 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.