What are the best practices for error handling and displaying files that begin with a period in PHP directories?

When working with directories in PHP, files that begin with a period (e.g., .htaccess) are considered hidden files and may not be displayed by default. To handle errors and display these files, you can use PHP's opendir() and readdir() functions to read the directory contents and then filter out files that begin with a period. You can also use error handling techniques such as try-catch blocks to handle any potential errors that may occur during the process.

<?php
$dir = "./your_directory_path";

try {
    $handle = opendir($dir);
    if ($handle) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && $file[0] != ".") {
                echo $file . "<br>";
            }
        }
        closedir($handle);
    } else {
        throw new Exception("Unable to open directory");
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>