/ Published in: PHP
This script reads a directory of files. After reading the directory specified it outputs the directory files. Upon output, supplied functions manipulate each filename and format it to make the output filename look better. e.g.: instead of output being 'my-good-looking-file.txt' it would look like 'My Good Looking File'.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// This script reads a directory of files. After reading the directory specified it outputs the directory files. // Upon output, supplied functions manipulate each filename and format it to make the output filename look better. // e.g.: instead of output being 'my-good-looking-file.txt' it would look like 'My Good Looking File'. // In order for the output to look better, you must put a hyphen (-) or an underscore (_) between each // word that you want to be separated. // function to strip off the file extension function strip_ext($name) { if($ext !== false) { } return $name; } // function to remove the hyphen or underscore from filename. function removeHyphen($filename) { $target = $filename; $patterns[0] = '/-/'; $patterns[1] = '/_/'; $replacements[0] = ' '; $replacements[1] = ' '; 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; foreach($cap as $key => $value) { } 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. while(($file = $mydir->read()) !== false) { // Security - remove "." and ".." files (directories) if ($file != "." && $file != "..") { // Output. This is what is actually outputed by the script and that shows // up on the screen (browser). echo "<p><a href='./dir/$file' title='Download $file'>".formatFile($file)."</a></p>"; } } $mydir->close();