I am trying to send Image to the PHP with along with other data.
PHP code:
if (isset($_FILES['image'])) {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
.....INSERT INTO DB.....WORKS FINE....
}
Can someone please let me know how to post other data from following iOS code along this image like "id", "index" etc so that I can access it via $_POST["id"]) and $_POST["index"] from above php page.
iOS code:
NSURL *baseURL = [NSURL URLWithString:BASE_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[baseURL URLByAppendingPathComponent:@"upload_image.php"]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"_187934598797439873422234";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream
" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = UIImageJPEGRepresentation(image, 90);
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection
sendAsynchronousRequest: request
queue: [NSOperationQueue new]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];
NSLog(@"%d == %@", code, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];