The sequence "upload, fail, edit, save, rename, succeed" tells me that it's likely a line-ending issue. Try running:

dos2unix original-uploaded-file


to see if that fixes the problem.

php scripts run only after renaming
I'm looking for an aphorism communicating the thought that "this works, but we can do better". Some high-falutin language like Shakespeare or Blake would be ideal.
