I created an iOS application which uploads an image taken by the camera to a server using the MKNetworkEngine
, as outlined in the following tutorial
If I set the host to posttestserver.com, a public POST testing server as the name suggests, it returns a URL and the path to the newly uploaded photo. Success!
Problem: I am unable to use my own PHP code to capture that file and save it on my webserver. I dont understand how posttestserver.com is successful in getting it, but my very straightforward php is not.
iOS side:
NSData *image = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 0.1);
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
@"woody", @"userName", @"testPassword", @"password", nil];
//WORKS:
self.flUploadEngine = [[fileUploadEngine alloc] initWithHostName:@"posttestserver.com" customHeaderFields:dic];
//Doesnt work:
self.flUploadEngine = [[fileUploadEngine alloc] initWithHostName:@"camerademander.com" customHeaderFields:dic];
NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"testApp", @"appID",
nil];
self.flOperation = [self.flUploadEngine postDataToServer:postParams path:@"/users/recieveStub3.php"];
[self.flOperation addData:image forKey:@"userfile" mimeType:@"image/jpg" fileName:@"upload.jpg"];
[self.flOperation addCompletionHandler:^(MKNetworkOperation* operation) {
NSLog(@"HTTP 200: success");
NSLog(@"%@", [operation responseString]);
}
errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {
NSLog(@"%@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
}];
[self.flUploadEngine enqueueOperation:self.flOperation];
php:
echo "Connected to server successfully.";
$uploaddir = '/photos/';
$file = basename($_FILES['userfile']['upload.jpg']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['upload.jpg'], $uploadfile)) {
echo "Successfully uploaded! :{$file}:";
}
This blocks my project from moving forward - I don't understand how posttestserver is able to get a result but I am not, especially when I am able to successfully connect to my own php and server.
Thanks much in advance!