What are common pitfalls when trying to create links for each file in a directory using PHP?

One common pitfall when trying to create links for each file in a directory using PHP is not properly handling file names that contain special characters or spaces. To solve this issue, you can use the `urlencode()` function to encode the file names before creating the links.

$directory = 'path/to/directory/';
$files = scandir($directory);

foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        $encodedFileName = urlencode($file);
        echo "<a href='$directory$encodedFileName'>$file</a><br>";
    }
}