Fatal error in wfDirectoryIterator.php: readdir(): false given
-
Hi,
We’re encountering a fatal error in the Wordfence plugin related to the
readdir()function. Here’s the full error message:PHP Fatal error: Uncaught TypeError: readdir(): Argument #1 ($dir_handle) must be of type resource or null, false given in /wp-content/plugins/wordfence/lib/wfDirectoryIterator.php:51After some debugging, we found that the issue occurs in the
scan()method of thewfDirectoryIteratorclass:protected function scan($dir) {
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
print_r($dir); // it returns an empty string ('').
$handle = opendir($dir);
$file_count = 0;
while ($file = readdir($handle)) {
...
}
}In our case, the
$dirvariable is'/'. When passed tortrim('/', DIRECTORY_SEPARATOR), it returns an empty string (''). As a result,opendir('')fails and returnsfalse, which leads to thereaddir()error since it’s expecting a valid directory handle.Could you please advise on how this should be handled, or whether a fix can be made to ensure the root directory
'/'is preserved correctly?Thanks in advance!
The topic ‘Fatal error in wfDirectoryIterator.php: readdir(): false given’ is closed to new replies.