This can be cleaned to use native PHP functions to devine what the directory structures are.