Title: Fatal error in wfDirectoryIterator.php: readdir(): false given
Last modified: April 22, 2025

---

# Fatal error in wfDirectoryIterator.php: readdir(): false given

 *  [addslashes](https://wordpress.org/support/users/addslashes/)
 * (@addslashes)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/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:
 *     ```wp-block-code
       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:51
       ```
   
 * After some debugging, we found that the issue occurs in the `scan()` method of
   the `wfDirectoryIterator` class:
 *     ```wp-block-code
       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 `$dir` variable is `'/'`. When passed to `rtrim('/', DIRECTORY_SEPARATOR)`,
   it returns an empty string (`''`). As a result, `opendir('')` fails and returns`
   false`, which leads to the `readdir()` 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!

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Support [wfpeter](https://wordpress.org/support/users/wfpeter/)
 * (@wfpeter)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18430030)
 * Hi [@addslashes](https://wordpress.org/support/users/addslashes/), thank-you 
   for your detailed look into what’s happening.
 * Could you please send Diagnostics so our team can see more about how WordPress
   is set up on your server? It will just help clarify if there’s an issue with 
   the configuration of the path itself, or we need to address something in the 
   plugin. Feel free to mention here any other notable factors in how the server
   is configured, such as whether symlinks are being used, etc.
 * You can send a diagnostic to **wftest @ wordfence . com** directly from the top
   of the **Wordfence > Tools > Diagnostics** page. Then click on **“Send Report
   by Email”**. Please** add your forum username **where indicated **and _respond
   here again after you have sent it._**
 * **NOTE:** It should look as follows – Screenshot of [Tools > Diagnostic > Send by Email](https://www.wordfence.com/wp-content/uploads/2021/09/diagnosticsendbyemail.png)
 * Many thanks,
   Peter.
 *  Thread Starter [addslashes](https://wordpress.org/support/users/addslashes/)
 * (@addslashes)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18430847)
 * Hi [@wfpeter](https://wordpress.org/support/users/wfpeter/)
 * I have just sent it.
   Thank you
 *  Plugin Support [wfpeter](https://wordpress.org/support/users/wfpeter/)
 * (@wfpeter)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18432155)
 * Thanks for sending that over [@addslashes](https://wordpress.org/support/users/addslashes/)!
 * We’ve seen this host come up a couple of times before configuring `ABSPATH` to`//`
   instead of something like `/home/user/public_html/my-wordpress/`. I have logged
   a development request with the team so that the plugin could work around it although
   it’s not the regular way to configure a WordPress site, so it’s not a common 
   problem. The quickest way for a resolution would be for your host to set `ABSPATH`
   to the root directory of your WordPress installation.
 * The host may be running a container setup, where WP core files **are** actually
   in `/` although two slashes showing up in the `ABSPATH` may still be a misconfiguration
   so it’s worth speaking with them.
 * You may need to disable the activity report to avoid the error until the path
   can be altered by your host, or we are able to issue an update.
 * Thanks,
   Peter.
    -  This reply was modified 1 year, 1 month ago by [wfpeter](https://wordpress.org/support/users/wfpeter/).
      Reason: Added information about possibly running the WordPress site in a container
 *  Thread Starter [addslashes](https://wordpress.org/support/users/addslashes/)
 * (@addslashes)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18433092)
 * Thank you for your help 🙂
 *  Plugin Support [wfpeter](https://wordpress.org/support/users/wfpeter/)
 * (@wfpeter)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18436317)
 * No worries [@addslashes](https://wordpress.org/support/users/addslashes/), please
   let us know if you hear more from them around how WordPress is configured on 
   their platform and if they were able to resolve the problem for you.
 * Peter.
 *  Thread Starter [addslashes](https://wordpress.org/support/users/addslashes/)
 * (@addslashes)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18437029)
 * Sure, I’ll let you know as soon as I hear anything from them.
 * Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Fatal error in wfDirectoryIterator.php: readdir(): false given’ is closed
to new replies.

 * ![](https://ps.w.org/wordfence/assets/icon.svg?rev=2070865)
 * [Wordfence Security - Firewall, Malware Scan, and Login Security](https://wordpress.org/plugins/wordfence/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordfence/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordfence/)
 * [Active Topics](https://wordpress.org/support/plugin/wordfence/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordfence/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordfence/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [addslashes](https://wordpress.org/support/users/addslashes/)
 * Last activity: [1 year, 1 month ago](https://wordpress.org/support/topic/fatal-error-in-wfdirectoryiterator-php-readdir-false-given/#post-18437029)
 * Status: not resolved