How can the issue of multiple div layers being created for each file in a directory be resolved in this PHP code?
Issue: The problem of multiple div layers being created for each file in a directory can be resolved by using a conditional check to only create a div layer if the current item is a file and not a directory.
<?php
$dir = "path/to/directory";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (is_file($dir . "/" . $file)) {
echo "<div>$file</div>";
}
}
closedir($dh);
}
}
?>
Keywords
Related Questions
- What are the potential pitfalls of storing guestbook entries in a text file versus a database?
- What are the potential pitfalls of using the same name for multiple input fields in PHP forms and how can they be avoided?
- What are some alternative ways to pass an unknown number of integer variables to a PHP function without using an array?