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"}
Ryan, thanks for the mod! I hope you don’t mind I updated the plugin with your code, ever so slightly modified.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.