将图像从ios上传到PHP到[关闭]

I am having an app in which I am uploading some data to web server.

Here is the code that I got from the links from google.

UIImage * img = [UIImage imageNamed:@"register_btn.png"];

    NSData *imageData = UIImageJPEGRepresentation(img, 90);
    NSString *urlString = @"http://tes.in/testservices/User.php";

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

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

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"
--%@r
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;filename=\a.jpg\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/json

"]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString =[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        //profile_photo_link
    NSLog(@"%@",returnString);

But I am not getting what exactly I need to pass here in the code.

My parameter for photo upload is "photo_link". I have given the link for php webservice.

I know there are lots of questions on this but I don't know where to pass my parameters and how to check the request string.

If possible please anyone can give me the code for PHP to decode image.

Please help me.

Thanks in advance.

Try this code :

-(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId
{            
    NSURL *strURL = @"http://webserver.com/....";//give your URL here

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:strURL];
    [request setHTTPMethod:@"POST"];

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

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Documents\"; filename=\%@.jpg
",imageId];
    [body appendData: [stringData dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the request
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];


    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"%@",returnString);
}

"Documents" is the folder name where you want to save an image to the backend server.

Good Luck!