Revision: 638
Updated Code
at July 24, 2006 18:37 by drewrockshard
Updated Code
// 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) { $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('./dir'); 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();
Revision: 637
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 23, 2006 23:26 by drewrockshard
Initial Code
<?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'. // 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) { $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('./dir'); 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(); ?>
Initial URL
http://www.deckerd.com
Initial Description
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'.
Initial Title
Read a directory and export pretty directory list
Initial Tags
javascript, list, files, directory
Initial Language
PHP