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);
    }
}
?>