I don't think you need rewinddir after opendir ?