If there is a problem with the file handling the resulting error messages are output corrupting the JSON response sent to the browser I have added "@" to suppress these messages so the response from the PHP is valid JSON.
I have also added a Json mime-type header to the output as it's good practice.
Suppressed error messages from fopen and fclose so that return format…
… is valid json and added json mime type to headers.
Added correct HTTP response codes from upload.php so that error handl…
it won't work with ie9 using ajax - there will be double request
I'm sorry. My bad. It's not about ajax, but there's still a bug in IE. Check it in Google Chrome Developer Console or in Firebug, using custom.html and original js files, try to upload a file (in html4 runtime, html5 won't work in ie9). It will cause two requests: http://s47.radikal.ru/i116/1210/3b/fb837cd20a02.png
IE9 (perhaps 6-9) will try to download second request as file: http://s019.radikal.ru/i640/1210/2a/1371cd055931.png if Content-Type is application/json
It is Internet Explorer bug
I'm going to merge this in. However header("Content-Type: application/json"); will have to be removed, as MaxKorz said it's problematic in old IEs.
One could probably play with Accept header, to get predictive behavior. However one important point here - upload.php is really just an example, you are free to alter it however you like or completely replace it with your own. Doesn't have to be so complex in most cases anyway.