se quello che hai postato e' effettivamente quello che fai e' ovvio che ti dia errore. Guarda come hai fatto il foreach prima su $file