Php ios映像上载到服务器不工作

Php ios映像上载到服务器不工作,php,ios,objective-c,image-uploading,Php,Ios,Objective C,Image Uploading,我正在尝试将图像从iso设备上载到我的服务器,这是源代码 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *smallImage = [info objectForKey:UIImagePickerControllerOriginalImage]; NSString*

我正在尝试将图像从iso设备上载到我的服务器,这是源代码

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{


    UIImage      *smallImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSString* serverurl = @"http://myserverurl/thumbnailCreator.php";
    NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSString *picPath = [path absoluteString];
    [self uploadImage2:serverurl :smallImage :picPath];

    [picker dismissModalViewControllerAnimated:YES];
}

-(bool)uploadImage2 :(NSString*)php :(UIImage*)image :(NSString*)picPath
{

    int h = image.size.height;
    int w = image.size.width;
    //uploadname =filename;
    NSString* filename = picPath;

    NSError *error;
    NSURLResponse *response;


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:php]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    [request addValue:filename forHTTPHeaderField:@"uploadfile"];


    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadfile\";filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[NSData dataWithData:UIImagePNGRepresentation(image)]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];




    // close the form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set request body
    [request setHTTPBody:body];


    // Make synchronous request
    NSData *data1 = [NSURLConnection sendSynchronousRequest:request
                                          returningResponse:&response
                                                      error:&error];
    NSString* outstr = @"NNOK";
    NSString *returnString;
    NSDictionary* json;
    if ([data1 length] > 0 && error == nil)
    {

        returnString = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

        //drill_debug_info("Response:%s",[returnString UTF8String]);
        NSError* error1;
        json = [NSJSONSerialization
                JSONObjectWithData:data1 //1
                options:kNilOptions
                error:&error1];
        outstr = [json objectForKey:@"Response"]; //2
    }


    if([outstr isEqualToString:@"OK" ])
    {
        return true;
    }
    else
    {
        return false;
    }


}
但是我看不到上传到服务器上的文件,并且总是得到
NNOK
结果。上面的代码可能有什么问题

这是我的php服务器

<?php
$target_path1 = "images/";
$a=array('Response'=>OK);
$b=array('Response'=>NOK);
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
if(!empty($_FILES)){
//print_r($_FILES);
$target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']);
//echo $target_path1;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path1)) {
    echo json_encode($a);
} else{
    // echo "Return Code: " . $_FILES["uploaded_file"]["error"]."size:".$_FILES["uploaded_file"]["size"];
    echo json_encode($b); 
}
}
?>

使用AFNetworking,可以这样尝试:

 #import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"

    NSString *stringUrl =@"http://www.myserverurl.com/file/uloaddetails.php?"
    NSString *string =@"http://myimageurkstrn.com/img/myimage.png"       
    NSURL *filePath = [NSURL fileURLWithPath:string];

   NSDictionary *parameters  = [NSDictionary dictionaryWithObjectsAndKeys:userid,@"id",String_FullName,@"fname",String_Email,@"emailid",String_City,@"city",String_Country,@"country",String_City,@"state",String_TextView,@"bio", nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:stringUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         [formData appendPartWithFileURL:filePath name:@"userfile" error:nil];//here userfile is a paramiter for your image 
     }
     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"%@",[responseObject valueForKey:@"Root"]);
         Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [Alert_Success_fail show];     

     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [Alert_Success_fail show];

     }];

你确定body和php不是nil吗,我怀疑application/octet stream,试试[@“Content Type:image/jpeg\r\n\r\n”dataUsingEncoding:NSUTF8StringEncoding];请发布uploadImage2方法的完整代码???@Rahul Shirphule我已经包含了uploadeimage2的完整代码@Alok是的,它不是空的。我进行了调试,我可以看到其中的一些价值。
 -(void)uploadImage
    {       
        NSData *imageData = UIImagePNGRepresentation(yourImage);

        NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1];

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

        [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    }