I want to send some key value pair while uploading the image to php server in iOS in objective c This is my iOS code :
request = [NSMutableURLRequest new];
request.timeoutInterval = 20.0;
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"%@.png\"\r\n; ", [self randomStringWithLength:5]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
UIImage *yourImage=[UIImage imageNamed:@"prem2.jpg"];
NSData *imageData = UIImagePNGRepresentation(yourImage);
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
NSError *error = nil;
NSURLResponse *responseStr = nil;
syncResData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseStr error:&error];
NSString *returnString = [[NSString alloc] initWithData:syncResData encoding:NSUTF8StringEncoding];
NSLog(@"ERROR %@", error);
//NSLog(@"RES %@", responseStr);
NSLog(@"Reture data %@", returnString);
with this code image upload successfully and I am getting the perfect ImageURL but i want to set some POST data along with this to the php script .this is my php script :
<?php
$target_path1 = "uploads/";
*$REQUEST['key'];//here i am not getting value*
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path1 = $target_path1 . basename( $_FILES['uploaded_file'] [ 'name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path1)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded to ".$target_path1;;
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploaded_file']['name']);
echo "target_path: " .$target_path1;
}
?>
how can i send value to this variable that can be fetch $REQUEST['key'] in PHP script.