Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
7 posts Page 1 of 1
Hi guys,

First of, I love Couch - Thank you so much for all the hard time and work you put in to this, bless!

I been trying to come up with a way to display the file size (In a formatted way) of uploaded files that I want to display on a page. This is the solution I came up with, maybe it's clunky but it works :)

Code: Select all
<?php
function filesize_formatted($path)
{

$path = preg_replace("!^htt.*/folder/!imsU","/var/www/yourdomain.com/folder/",$path);

    $size = filesize($path);
    $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $power = $size > 0 ? floor(log($size, 1024)) : 0;
    return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];

}
?> 
                    <cms:php>
                      $name = pathinfo('<cms:show file />')['filename'];
                        $fullname = '<cms:show file />';
                        $ext = pathinfo('<cms:show file />')['extension'];
                        echo "<span class=\"fileinfo\">$ext </span>File | ".filesize_formatted("$fullname");
                    </cms:php>


Happy Friday ;)
Thanks @quickiez :)

Perhaps you'll also find the following threads dealing with the same issue (albeit on the backend) useful -
viewtopic.php?f=8&t=7440
viewtopic.php?f=8&t=11691

In particular, you may want to use the following bit of code in your solution to avoid hardcoding the file paths (this making it generic) -
Code: Select all
global $Config;
$filepath = str_replace( $Config['k_append_url'] . $Config['UserFilesPath'],  $Config['UserFilesAbsolutePath'], $file);
try {
    $filesize = filesize($filepath);
}
KK wrote: threads dealing with the same issue (albeit on the backend) useful -
viewtopic.php?f=8&t=11691


Thank you, KK, for quoting my thread. I have added a generic cms:func to make use on front-end as well. It also extends functionality to signal if file is not found. Moreover, paths can be provided in url form (http://example.com/folder/myfile.jpg) or as disk paths.
Hi trendoman, the Github link you've provided for the filesize function gives 404, is there any chance to upload the file here? I've already tried to handle the issue according to the setup here: viewtopic.php?f=2&t=10193&p=35560 . Unfortunately, there is a small glitch which resets the filesize value for the first record.
Thank you very much
gali wrote: Hi trendoman

Hi,

Something like this screenshot that was captured in admin panel? Where else do you need it working?

2020-04-20_083810.png
2020-04-20_083810.png (4.46 KiB) Viewed 2888 times
Actually, I intend to have a filesize displayed at the front end, as shown below...
filesize.png
Front end filesize display
filesize.png (56.62 KiB) Viewed 2883 times
gali wrote: Actually, I intend to have a filesize displayed at the front end, as shown below...
filesize.png


Sent PM. Maybe you also need a function to get file's extension automatically? In your screenshot there is a 'PDF' ext, but it can be automated to get correct ext if you allow client to upload other types of files.
7 posts Page 1 of 1
cron