Thanks adrianh for this - it does make the code cleaner, and make more sense than what I was doing. It's actually something I've tried before but it still dies there though.
It's running on apache & the error log just says my script died at the 'upload' line. Weird isn't it?