A simple snippet to grab the file size, date modified and file icon for a given file. I have used it to generate a list of files by using it in a NewsListing (Ditto) template, where the filepath is passed as a TV to the newslisting template (each document being listed has a TV called ’file’):
Example use in a NewsListing template (we’re outputting items inside a <dl>):
<dt><a href="[+tvfile+]">[+title+]</a></dt>
<dd>
[+summary+]
[!FileDetails?file=`[+tvfile+]`!]
</dd>
Real life example:
http://www.aquavision.co.uk/documentation
I believe this is preferable to to uploading documents to a directory and just listing the directory, because you can use the document to describe the file meta data, and also files can be searched, ordered and listed like any other document. And it makes a lot more sense to see the files in the document tree.
I have attached a folder of icons called ’fileicons’ - copy this folder to /assets/images/.
Incidently, I have used the file extension to determine the file type; a better option would be to use the Mime type, but I decided this would require too much overhead. If anyone knows of a clean and simple way to determine mimetype please let me know!
/*
Snippet Name: FileDetails
Short Desc: Returns file information for supplied file path (size, date modified, file icon).
Created By: [email protected]
Last modified: 26/06/2006
Version: 1.0
Example call: [!FileDetails?file=`/assets/myfile.pdf` &tpl=`chunkName` &icondir=`/assets/fileicons/`!]
Note: The parameter file is required or you will recieve no results.
Params:
&file = file path to use (must be a root relative path) (string)
&tpl = Chunk name to use for formatting file details (string)
&icondir = path to the file icons directory (root relative or absolute path) (string)
&iconsize = "small" | "standard" (string)
$dtformat = date format (string)
&bytemode = "SI"|"IEC" [Metric where 1 Kb=1000 bytes or IEC where 1 Kb=1024 bytes] (string)
&digits = number of digits for the file size (integer)
*/
/* Globals */
global $base_path;
global $site_url;
/* Parameters */
$file = isset($file)? "$file" : '';
$tpl = isset($tpl)? "$tpl" : '[+icon+] [+size+] | [+filedate+]';
$icondir = isset($icondir)? "$icondir" : $site_url.'assets/images/fileicons/';
$iconsize = isset($iconsize)? "$iconsize" : 'small';
$dtformat = isset($dtformat)? "$dtformat" : 'F d Y';
$bytemode = isset($bytemode)? "$bytemode" : 'SI';
$digits = isset($digits)? "$digits" : '3';
$file = $base_path.(substr_replace($file, '', 0,1));
$output = '';
if ($file != "") {
# grab file details
$file = getFileDetails($file, $icondir, $dtformat, $digits, $bytemode, $iconsize);
# map to template
$tpl = $modx->getChunk($tpl);
$fields = array('[+size+]', '[+icon+]', '[+filedate+]');
$values = array($file['size'],$file['icon'],$file['date']);
$output .= str_replace($fields,$values,$tpl);
}
return $output;
/* ---------------------------------------------------
Functions
--------------------------------------------------- */
/* courtesy of [email protected] */
function formatbytes($val, $digits = 3, $mode = "SI", $bB = "B"){ //$mode == "SI"|"IEC", $bB == "b"|"B"
$si = array("", "k", "M", "G", "T", "P", "E", "Z", "Y");
$iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi");
switch(strtoupper($mode)) {
case "SI" : $factor = 1000; $symbols = $si; break;
case "IEC" : $factor = 1024; $symbols = $iec; break;
default : $factor = 1000; $symbols = $si; break;
}
switch($bB) {
case "b" : $val *= 8; break;
default : $bB = "B"; break;
}
for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++)
$val /= $factor;
$p = strpos($val, ".");
if($p !== false && $p > $digits) $val = round($val);
elseif($p !== false) $val = round($val, $digits-$p);
return round($val, $digits) . " " . $symbols[$i] . $bB;
}
# Outputs image link dependant of passed in mime type.
function getFileIcon($ext, $icondir, $iconsize="normal") {
# Determine if we need small or standard sizes file icons
if ($iconsize === "small") {
$suffix = "_s.gif";
} else {
$suffix = ".gif";
}
$ext = strtolower($ext);
# Case statement based around extension type:
switch($ext) {
#IMAGES:
case 'gif': case 'jpg': case 'jpeg': case 'png': case 'tif': case 'tiff': case 'bmp':
$icon = 'icon_image'.$suffix;
$alt = 'Image';
break;
#EXCEL:
case 'xls':
$icon = 'icon_excel'.$suffix;
$alt = 'Microsoft Excel';
break;
#HTML:
case 'html': case 'htm':
$icon = 'icon_html'.$suffix;
$alt = 'HTML';
break;
#PDF:
case 'pdf':
$icon = 'icon_pdf'.$suffix;
$alt = 'Adobe PDF';
break;
#POWERPOINT:
case 'ppt':
$icon = 'icon_pp'.$suffix;
$alt = 'Microsoft Powerpoint';
break;
#TEXT
case 'rtf': case 'txt': case 'css': case 'js': case 'xml':
$icon = 'icon_text'.$suffix;
$alt = 'Text';
break;
#WORD
case 'doc':
$icon = 'icon_word'.$suffix;
$alt = 'Microsoft Word';
break;
#DEFAULT
default:
$icon = 'icon_generic'.$suffix;
$alt = 'Generic';
break;
}
$imageUrl = '<img src="'.$icondir.$icon.'" alt="'.$alt.'" title="'.$alt.'" />';
return $imageUrl;
}
function getFileDetails ($filepath, $icondir, $dtformat, $digits, $bytemode, $iconsize) {
$file = array();
If (is_file($filepath)) {
// get file size
$file['size'] = formatbytes(filesize($filepath), $digits, $bytemode, "B");
//get path info
$pathinfo = pathinfo($filepath);
// get file extension
$file_ext = $pathinfo['extension'];
//get file icon
$file['icon'] = getFileIcon($file_ext, $icondir, $iconsize);
//get file date last modified
$file['date'] = date ($dtformat, filemtime($filepath));
}
return $file;
}