don't segfault when trying to fdopen() a fd for a dir (closes #22259)

Patch from Brian Kearns.