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!
In your iOS side,change the key "userfl" to "userfile"
[self.flOperation addData:image forKey:@"userfile" mimeType:@"image/jpg" fileName:@"upload.jpg"];
In your php side,you should chang your code like this:
echo "Connected to server successfully.";
$uploaddir = '/photos/';
$file = basename($_FILES["userfile"]["name"]);
$uploadfile = $uploaddir.$file;
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploadfile)) {
echo "Successfully uploaded! :{$file}:";
}
I'm not a PHP Programmer, after look at w3schools's PHP File Upload Tutorials, write the answer, hope it works :)
UPTATE:
ok,updated my answer to get more info for your qusetion.(I have test on my server,it works well)
use the php code below and see what you get
if($_FILES){
$uploaddir = './photos/';
$file = basename($_FILES["userfile"]["name"]);
$uploadfile = $uploaddir.$file;
if(!file_exists($uploaddir)){
if(!mkdirs($uploaddir)){
echo "Fail to creat uploaddir";
exit();
}
}else if( file_exists($uploadfile)){
echo "Upload file exists";
exit();
}
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploadfile)) {
echo "Successfully uploaded! :{$file}:";
}else{
echo "Failed uploaded! :{$file}:";
}
}else {
echo "Uploaded empty file";
}
function mkdirs($dir, $mode = 0777) {
if (is_dir ( $dir ) || @mkdir ( $dir, $mode ))
return TRUE;
if (!mkdirs ( dirname ( $dir ), $mode ))
return FALSE;
return @mkdir ( $dir, $mode );
}