mbgritten Posted July 28, 2009 Share Posted July 28, 2009 Hello, I'm trying to get a script working which will write a list of links to files within any directory the script is dropped into, but following these criteria: a) Certain file extensions should be hidden (e.g. no *.gif files showing) b) Some files should be filtered using DOS */? criteria (e.g. config*.*) c) No directories should show d) The file listing should hide the file extension (e.g. myfile.php should show as myfile) e) The file listings should be capitalised I've got this far: <?php // function to strip off the file extension function strip_ext($name) { $ext = strrchr($name, '.'); if($ext !== false) { $name = substr($name, 0, -strlen($ext)); } return $name; } // function to remove the hyphen or underscore from filename. function removeHyphen($filename) { $target = $filename; $patterns[0] = '/-/'; $patterns[1] = '/_/'; $replacements[0] = ' '; $replacements[1] = ' '; $filename = preg_replace($patterns, $replacements, $target); return $filename; } // function to capatalize the first character of each word. Must be called after // the removal of the hyphen or underscore function capFirstWord($word) { $cap = $word; $cap = explode(' ', $cap); foreach($cap as $key => $value) { $cap[$key] = ucfirst($cap[$key]); } $word = implode(' ', $cap); return $word; } // Formats the file. This is the main function that calls all functions explained above. function formatFile($name) { $name = strip_ext($name); $name = removeHyphen($name); $name = capFirstWord($name); return $name; } // Sets up the directory you would like to use as the "reading" directory. $mydir = dir('./'); // setup a blacklist of extension to ignore: $extension_blacklist = array("gif", "js", "css", "config*.*"); while (($file = $mydir->read()) !== false) { // get information about the file: $file_info = pathinfo($file); // Security - remove "." and ".." files (directories) if ($file != "." && $file != ".." && $file != "printpage.php" && !in_array($file_info['extension'], $extension_blacklist)) { // Output. This is what is actually outputed by the script and that shows // up on the screen (browser). echo "<li><a href='./$file'>".formatFile($file)."</a></li>"; } } $mydir->close(); ?> But I'm struggling with: a) Hiding the directories b) Filtering out certain files using the DOS wildcards Can anyone help in either showing me what to do or pointing me in the right direction? Thank you in advance!! Mark Link to comment https://forums.phpfreaks.com/topic/167758-solved-listing-files-but-hiding-directories/ Share on other sites More sharing options...
mbgritten Posted July 28, 2009 Author Share Posted July 28, 2009 I've got the point at which I've hidden the directories, but now need some help sorting the output alphabetically. Here's where I'm at: <?php // function to strip off the file extension function strip_ext($name) { $ext = strrchr($name, '.'); if($ext !== false) { $name = substr($name, 0, -strlen($ext)); } return $name; } // function to remove the hyphen or underscore from filename. function removeHyphen($filename) { $target = $filename; $patterns[0] = '/-/'; $patterns[1] = '/_/'; $replacements[0] = ' '; $replacements[1] = ' '; $filename = preg_replace($patterns, $replacements, $target); return $filename; } // function to capatalize the first character of each word. Must be called after // the removal of the hyphen or underscore function capFirstWord($word) { $cap = $word; $cap = explode(' ', $cap); foreach($cap as $key => $value) { $cap[$key] = ucfirst($cap[$key]); } $word = implode(' ', $cap); return $word; } // Formats the file. This is the main function that calls all functions explained above. function formatFile($name) { $name = strip_ext($name); $name = removeHyphen($name); $name = capFirstWord($name); return $name; } // Sets up the directory you would like to use as the "reading" directory. $mydir = dir('./'); // setup a blacklist of extensions to ignore: $extension_blacklist = array("gif", "js", "css"); $filter = array("config.php", "printpage.php"); while (($file = $mydir->read()) !== false) { // get information about the file: $file_info = pathinfo($file); // Security - remove "." and ".." files (directories) if ($file != "." && $file != ".." && !in_array($file, $filter) && !in_array($file_info['extension'], $extension_blacklist)) if(!is_dir($file)) { // Output. This is what is actually outputed by the script and that shows // up on the screen (browser). echo "<li><a href='./$file'>".formatFile($file)."</a></li>"; } } $mydir->close(); ?> Can anyone help me with the directory list sorting? Thanks, Mark Link to comment https://forums.phpfreaks.com/topic/167758-solved-listing-files-but-hiding-directories/#findComment-884714 Share on other sites More sharing options...
trq Posted July 28, 2009 Share Posted July 28, 2009 You might want to take a look at glob. While your at it, those other functions can be made redundant using basename, str_replace and ucwords. Link to comment https://forums.phpfreaks.com/topic/167758-solved-listing-files-but-hiding-directories/#findComment-884747 Share on other sites More sharing options...
patrickmvi Posted July 28, 2009 Share Posted July 28, 2009 You should put all of your filenames in an array and then use the sort function to put them in alphabetical order and then display them out of the array. Here's a link to the sort function: http://www.php.net/sort Link to comment https://forums.phpfreaks.com/topic/167758-solved-listing-files-but-hiding-directories/#findComment-884817 Share on other sites More sharing options...
mbgritten Posted July 28, 2009 Author Share Posted July 28, 2009 Thanks for the help! Problem now sorted. Mark Link to comment https://forums.phpfreaks.com/topic/167758-solved-listing-files-but-hiding-directories/#findComment-884915 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.