/** * Return a directory listing as a PHP array. * @param string $directory The directory to return a listing of. * @param integer $depth The private argument used to limit recursion depth. * @return array representing the directory structure. */ function directory_listing($directory = '', $depth = 1) { // We return an empty array if the directory is empty $result = array('$type' => 'folder'); // We won't recurse below MAX_DEPTH. if ($depth > MAX_DEPTH) { return $result; } $path = empty($directory) ? working_directory() : $directory; // We'll open the directory to check each of the entries if ($dir = opendir($path)) { // We'll keep track of how many file we process. $count = 0; // For each entry in the file while (($file = readdir($dir)) !== false) { // Limit the number of files we process in this folder $count += 1; if ($count > MAX_FILES_PER_FOLDER) { return $result; } // Ignore hidden files (this includes special files '.' and '..') if (strlen($file) && $file[0] == '.') { continue; } $filepath = $path . DIRECTORY_SEPARATOR . $file; if (filetype($filepath) == 'dir') { // We'll recurse and add those results $result[$file] = directory_listing($filepath, $depth + 1); } else { // We'll check to see if we can read any image information from // the file. If so, we know it's an image, and we can return // it's metadata. $imageinfo = @getimagesize($filepath); if ($imageinfo) { $result[$file] = array('$type' => 'image', 'metadata' => array('width' => $imageinfo[0], 'height' => $imageinfo[1], 'mimetype' => $imageinfo['mime'])); } elseif ($extension = strrpos($file, '.')) { $extension = substr($file, $extension); if ($extension == '.htm' || $extension == '.html') { $result[$file] = array('$type' => 'html'); } else { $result[$file] = array('$type' => 'text'); } } else { $result[$file] = array('$type' => 'document'); } } } closedir($dir); } return $result; }
<?php //-------------------------------------------------------------- define('INCLUDE_CHECK', true); //-------------------------------------------------------------- require_once '../models/functions.php'; $list = directory_listing('.'); echo "<p>List of files:</p>"; foreach ($list as $file) { if (strrpos($file, '.txt') > -1) { echo '<a href="' . $file . '">' . $file . "</a><br />\n"; } }