Php Programming Code Examples
Php > File Manipulation Code Examples
List files in a directory, no subdirectories
List files in a directory, no subdirectories
<?
echo "<form>";
//Looks into the directory and returns the files, no subdirectories
echo "<select name='yourfiles'>";
//The path to the style directory
$dirpath = "/path/to/your/directory";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
//Don't list subdirectories
if (!is_dir("$dirpath/$file")) {
//Truncate the file extension and capitalize the first letter
echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';
}
}
closedir($dh);
//Close Select
echo "</select>";
echo "</form>";
?>