Had fun today with readdir(), this function expects to be passed a valid handle that was created with opendir(). The php documentation states: “Returns the entry name on success or FALSE on failure”. Trouble can happen if you assume opendir() is going to return a valid resource. If opendir() is given an invalid directory and fails then false is returned. Passing anything but a resource to readdir() results in the function returning null, not false.

while (false !== readdir())

So today I came across a case of a recursive while loop conditioned on readdir() not being false, since another bug had passed opendir() an invalid directory. That created a cool loop that filled the server logs with PHP Warnings from readdir().

Based on a bug report that links to functions.internal.php by convention functions passed invalid parameters will return NULL.

Filed under “Learned something new today”