// snippet fSize
// example usage: [!fSize? &fn=`assets/images/austinpowers-01.jpg`!]
$output = null;
// from: http://ch2.php.net/manual/en/function.filesize.php#77518
function size_readable ($size, $retstring = null) {
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if ($retstring === null) { $retstring = '%01.2f %s'; }
$lastsizestring = end($sizes);
foreach ($sizes as $sizestring) {
if ($size < 1024) { break; }
if ($sizestring != $lastsizestring) { $size /= 1024; }
}
if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
return sprintf($retstring, $size, $sizestring);
}
if($fn) {
if( !stristr($fn, '.htaccess') && !stristr($fn, '.php') && !stristr($fn, '.inc') && !stristr($fn, '.htm') ) {
$fullPath = $modx->config['base_path'] . $fn;
if(file_exists($fullPath)) {
$fsc = filesize($fullPath);
$fs = size_readable($fsc);
$baseURL = MODX_BASE_URL;
$shortFN = basename($fn);
$output = "<a href='$baseURL$fn' title='download $fn ($fs)'>$shortFN ($fs)</a><br />";
return($output);
}
}
}
This simple snippet checks if the file really exists, if it really is one of "allowed" file-type, and formats the filesize from bytes to KB/MB/GB if necessary, and output a simple link. That’s just one of many ways... you could also scan an entire directory and only list images etc. For single files however, this should work fine.