Pleth Reply #1, 3 years, 2 months ago
Just wanted to share a snippet I have been using to return unordered lists based on documents in a given directory. With a couple of modifications it has been coming in handy for several instances. I had actually used a variation of this previously to populate an unordered list styled for a photogallery.
Anyway, the advantage has been that is simplifies updating of lists (meeting minutes, newsletters, etc..) for the end user. They just need to upload a new document into the appropriate directory. Hope it is useful for someone.
Anyway, the advantage has been that is simplifies updating of lists (meeting minutes, newsletters, etc..) for the end user. They just need to upload a new document into the appropriate directory. Hope it is useful for someone.
<?php
/* -------------------------------------------------------------
:: Snippet: Returns Directory Contents (Document Manager)
----------------------------------------------------------------
Short Description:
Returns Directory Contents (Documents)
Date:
11/18/2008
----------------------------------------------------------------
:: Example Call
----------------------------------------------------------------
[!documentMan? &Location=`FileName` &docSort=`sort`!]
- A call that describes the directory inside of assets/files/ that you want called in.
- docSort, use 'sort' for alphanumeric sorting or 'rsort' for reverse alphanumeric sorting.
------------------------------------------------------------- */
// Shows all files in the directory (assumes you are in assets/files/),
$newline = '';
$dir = $modx->config['(site_url)'].'assets/files/'.$Location.'/'; // set path to files
$dir_array = array(); // main array - contains all file names in directory
// open directory and parse file list
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
// iterate over file list to create full directory array
while (($filename = readdir($dh)) !== false) {
if (($filename != ".") && ($filename != "..") && ($filename !="WS_FTP.LOG") && (!preg_match('/^.thumb_/', $filename))) { // skip self, parent, and ftp log and thumb prefix if returning images
$dir_array[] = $filename; // add the filename to the array
}
}
closedir($dh); // close directory
}
$docSort($dir_array,SORT_STRING); // sorting.
$n = count($dir_array); // total number of files -- might want this for something
$output = '';
$output .= '<h3>'.$Location.','.$n.' Documents</h3>';
$output .= '<ul>';
foreach ($dir_array as $value) { // iterate through array of filenames.
$output .= '<li><a href="'.$dir.''.$value.'">'.$value.'</a></li>';
}
$output .='</ul>';
}
return $output;
?>