in reply to multipart/form-data files

Is that an exact copy and paste of your code, or did you re-type it? Because you define $userdir but then use $user_dir in the rest of the lines.

---
It's all fine and dandy until someone has to look at the code.