Your PHP is looking for the field name userfile
, but your Objective-C is using attachment
. You must use the same field name on both platforms. I also assume the "<$php
" was just a typo and that you intended "<?php
".
A couple of other improvements you might want to consider:
I would suggest that you might want to change your PHP to return JSON rather than just writing text strings. It will be easier for your Objective-C code to parse the responses and differentiate between various errors and success.
For example, your if successful, your PHP might do the following:
$result = array("success" => true, "code" => 0, "message" => "success");
Or if you wanted to log those additional values, as in your existing code sample, you could:
$result = array("success" => true, "code" => 0, "message" => "success", "postsize" => $postsize, "canupload" => $canupload, "tempdir" => $tempdir, "maxsize" => $maxsize);
If unsuccessful, you might do:
$result = array("success" => false, "code" => 1, "message" => "file not found");
or
$result = array("success" => false, "code" => 2, "message" => "file too large");
Regardless of which
$result
is chosen, when done, you should JSON encode it andecho
it (rather than echoing the simple text string):echo json_encode($result);
Clearly, use whatever codes and messages you want, but the idea is to return JSON, which can be easily parsed to determine if the upload request was successful, and if not, then why. Trying to parse simple text responses from the server will be inherently fragile.
Anyway, your Objective-C can then parse this response and just check the
success
orcode
values and handle these scenarios appropriately.I would not suggest having the PHP save the uploads in the same folder as the PHP, itself. At the very least, I'd create a dedicated subdirectory for the uploads. I'd personally choose a directory completely outside the web server's directory structure.