ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Anyone know of a way to output file size?

September 24, 2010 12:00am

Subscribe [3]
  • #1 / Sep 24, 2010 12:00am

    Tony Krol

    21 posts

    I’ve tried this (documentation states it is compatible w/ EE2), and found this (no EE2 version). The human filesizer is installed, but it doesn’t actually do anything. It only outputs a static 4KB, and ignores the custom file field that I have between the open and closing tags.

    Has anyone had any luck with this in EE2?

    I suppose I can just probably tool around with some php and write something fairly simple to convert the byte size of the linked file.. meh…. {this is so}{/much easier though}

    <?php 
    function formatBytes($bytes, $precision = 2) { 
        $units = array('B', 'KB', 'MB', 'GB', 'TB'); 
       
        $bytes = max($bytes, 0); 
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
        $pow = min($pow, count($units) - 1); 
       
        $bytes /= pow(1024, $pow); 
       
        return round($bytes, $precision) . ' ' . $units[$pow]; 
    } 
    ?>
  • #2 / Sep 24, 2010 12:33am

    Cheif

    626 posts

    I’ve been looking at these 2 offerings, just haven’t got around to it yet:

    http://devot-ee.com/add-ons/ss-human-filesize/

    http://devot-ee.com/add-ons/file-oracle/

    Both claim EE2 compatibility

  • #3 / Sep 24, 2010 10:10am

    Ingmar

    29245 posts

    Yes, a third-party add-on (or some custom PHP code, of course) would be the way to go. Does that help, Tony?

  • #4 / Sep 24, 2010 1:03pm

    Tony Krol

    21 posts

    I suppose in answering the question that was posted in the title of the thread, a third party plugin, or a custom php solution would be the way to go….

    More specifically, the two mentioned plugins by SS seem to almost get me there. Unfortunately the plugins only support the relative path, or server path. But I want to display the human size of a file that is a custom field of the type “file.”

    The File Oracle plugin does work when I hard code the file name, while the Human Filesizer does not.

    Any insight how to display a relative file path, or remove the http://www.domainname.com/ from the rendered code that the {ee_short_tag} produces. I’m going to try a find and replace plugin and see if I can find the “http://www.domainname.com” and replace it with nothing. This may get me there, in theory.

    I’ll post the results shortly. However if any other brains see a reason this may not work in the mean time, I’m open to other suggestions of course.

  • #5 / Sep 24, 2010 1:40pm

    Tony Krol

    21 posts

    So after some experimenting with Low - Replace (http://loweblog.com/software/low-replace/)

    I wrote this:

    <a class="dl" rel="tag" href="{mix_zip}" title="{title}">download ↓ ({exp:file_oracle file={exp:low_replace find='http://domainhere.com/' replace='/'}{mix_zip}{/exp:low_replace}}{human_size}{/exp:file_oracle})

    If I take the low_replace tag and try it outside of the file_oracle tag pair, it does find and replace the text correctly. But the file_oracle plugin does not see this as a relative path within when used in conjunction with the low_replace tag.

    As I said, a static coded relative path for the file makes the file_oracle plugin work, and display the correct file size. Is there any way that anyone knows of to display a relative path with an ee short tag. So that {mix_zip} displays a relative path without the http://domainname.com in front?

  • #6 / Sep 24, 2010 3:02pm

    Tony Krol

    21 posts

    Here was my solution, assisted by my good friend Dave Rau.

    Turn PHP on for the template.

    Use this function:

    <?php
    function get_file_size($file) {
            $file = str_replace('http://yourdomain.com/', '', $file);
            $size = filesize($file);
            $units = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
     
            foreach($units as $pow => $unit) {
                    if($size / pow(1024, $pow) < 1024)
                            return number_format($size / pow(1024, $pow)) . ' ' . $unit;   
            }
    }
    ?>

    Use this to output the file size:

    <?php echo get_file_size('{custom_field}'); ?>

    See it in action here: http://djop1.com/ (under development)

  • #7 / Sep 25, 2010 9:46am

    Sue Crocker

    26054 posts

    Thanks for posting your solution, Tony. I know this will help others who want the same functionality.

    Don’t hesitate to post again as needed.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases