We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Filesizer plugin, a couple (unofficial) mods

Development and Programming

Ryan M.'s avatar
Ryan M.
1,511 posts
18 years ago
Ryan M.'s avatar Ryan M.

Back on this thread, ExpressionEngineer wrote a plugin called Filesizer, which happened to be something I was looking for today.

Lealea had a request on that thread to make the filesizer output jump from kb to MB automatically. I made that mod as well as others such as outputting the file extension if desired (which is shown in the pic), and easier integration with Mark Huot’s File module.

I thought it might be useful to outline what I did for the kb/MB issue, in case anyone wants to make a quick (unofficial) mod to their copy of the plugin. See the attached pic for the kb/MB output, which is automatic.

Find this:

if ( @file_exists($file) )
        {
            $fs = @filesize($file);
            
            // one could easily add more units here later on..
            if ( $units == "mb" ) $div = 1024 * 1024;
            if ( $units == "kb" ) $div = 1024;
            
            if ( $decimals == 0)
            {
                $fs = ceil($fs / $div);
            }
            elseif ($decimals > 0)
            {
                $fs = round(($fs / $div), $decimals);
            }
        }

        return $this->return_data = $fs;
    } // end filesizer

Change to:

if ( @file_exists($file) )
        {
            $fs = @filesize($file);
            $units = array("b", "kb", "MB", "GB");
            $units_count = count($units);
            
            for($unit = 0; $unit < $units_count && $fs >= 1024; $unit++)
                        {
                        $fs /= 1024;
                        }
            // Don't include decimals if size is b or kb
                $decimals = $unit < 2 ? 0 : $decimals;
                 $sizedata = number_format($fs, $decimals) . " " . $units[$unit];
        }
            return $this->return_data = $sizedata;
        }
    } // end filesizer

Then you can comment out the line about units (line 27) and just output a tag like this, with no units declared:

{exp:filesizer filename="{your_file}" upload_pref="3"}
       
stinhambo's avatar
stinhambo
1,268 posts
18 years ago
stinhambo's avatar stinhambo

I get a big blank Plugin page if I implement these changes…

The original plugin works fine though.

       
ExpressionEngineer's avatar
ExpressionEngineer
148 posts
18 years ago
ExpressionEngineer's avatar ExpressionEngineer

Ryan, thanks for the mod! I hope you don’t mind I updated the plugin with your code, ever so slightly modified.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.