if(is_dir($mydir->path.$file)) {
does not produce the desired effect for me; I changed it to
if(is_dir("$mydir->path/$file")) {
and it now works. Possibly a typo, or am I missing something? Would also probably work as
if(is_dir($mydir->path.'/'.$file)) {
if you wanted to keep it that way.