I'm having some trouble getting some code I have acquired working, my experience with php/html is 'limited' at best so I hope I'm not looking like too much of a noob by asking this.
The problem I am having is not being able to upload a small image to a web-server. I have played around with the following for the past 4-5 hours, it's nearly 2am here in the UK and the only progress I have made has been changing
[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
to
[NSString stringWithFormat:@"enctype=\"multipart/form-data\"; boundary=%@",boundary];
This gets my Server to stop giving an internal error every time i send something.
the code is below, it is on a number of pages on this site and I cannot find the original author to give credit. I am currently getting an empty array when I have print_r($_FILES); in the receiving php file.
Any help what so ever would be massively appreciated, thanks in advance.
NSString *urlString = @"http://mysite/upload_image.php";
UIImage * img = [UIImage imageNamed:@"mypic.png"];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"enctype=\"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:[@"Content-Disposition: attachment; name=\"image\"; filename=\"davesfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);