What are some potential pitfalls to be aware of when using readdir() function in PHP?

One potential pitfall when using the readdir() function in PHP is that it may return false when there are no more files in the directory, which can lead to an infinite loop if not handled properly. To avoid this, you should check for the false return value and break out of the loop when necessary.

$dir = "/path/to/directory";

if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        // Process the file here
    }
    closedir($handle);
}