I am always watching the MODx forums for these useful little gems. Susan Ottwell has created a Filefinder snippet in which you can return an unordered list of documents based on file type from a given directory. This is a version 1, but I knew I could immediately put this to good use.
<?php
/** Filefinder - create a menu from the filesystem
* Wayfinder for files
* Author - sottwell@sottwell.com
* Created - Sept 2009
* Version 1
*
* @Parameters:
* &directory - the name of the folder the files are in; default ''
* &basedir - the location of the folder; default 'assets/files'
* &fullpath - the full path to the folder; default 'MODX_BASE_PATH + $basedir
* &filetype - file type of files to list for download; default '.pdf'
*
*@Usage
* to list .pdf files in the assets/files directory
* [!Filefinder!]
* to list .pdf files in assets/files/downloads
* [!Filefinder? &directory=`downloads`!]
* to list .doc files in assets/files/docs
* [!Filefinder? &directory=`docs` &filetype=`.doc`!]
* to list .whatever files in the "downloads" directory in the web root
* [!Filefinder? &basedir=`downloads` &filetype=`.whatever`!]
**/
// set up directory path
$directory = isset($directory) ? $directory . '/' : '';
$basedir = isset($basedir) ? $basedir . '/' : 'assets/files' . $directory;
$fullpath = isset($fullpath)? $fullpath .'/' : MODX_BASE_PATH . $basedir;
$fullURL = MODX_BASE_URL . $basedir;
// list eligible files
$filetype = isset($filetype) ? $filetype : '.pdf';
if ($handle = opendir($fullpath)) {
$list = '<ul>';
while (false !== ($file = readdir($handle))) {
if ((strpos($file, '.') != 0) && (strpos($file, $filetype, strlen($file) -
strlen($filetype)) !== false)) {
$list .= '<li><a href="' . $fullURL . $file . '">' . $file . '</a></li>';
}
}
closedir($handle);
}
$list .= '</ul>';
return $list;
?>


