While you're editing this file would you mind moving the getcwd() call above the first foreach and cache the result? Makes no sense to call this at every iteration.