In PHP, we can list files with no sub-directories. To understand this we will take a example. Suppose we have a directory demo whose files we want to list inside a drop down box. We will take a form then open the directory whose files we want to display by using the function opendir(). Then we display all the files inside drop down. To do this you need to use the following script:
<?
echo "";
//Looks into the directory and returns the files, no subdirectories
echo "<select name='yourfiles'>";
//The path to the style directory
$dirpath = "demo";
$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>";
?>
0 Comment(s)