Can you see the following code, I hope you find it useful.
Here is the iOS code
-(void)createConnectionRequestToURL:(NSString *)urlStr withImage:(UIImage*)image withImageName:(NSString*)imageName { NSData *imageData = UIImageJPEGRepresentation(image, 90); NSString *urlString = urlStr; // setting up the request object now NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = [[NSString alloc]init]; 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:[@"Content-Disposition: form-data; name=\"file\"; filename=\"test.png\"rn" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Type: application/%@.jpg\r\n\r\n",imageName] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; //Using Synchronous Request. You can also use asynchronous connection and get update in delegates NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"--------%@",returnString); }
Find server-side (PHP) encoding for the image. Download with a random name. he will also give a link to the image as an answer.
//Create a folder named images in your server where you want to upload the image. // And Create a PHP file and use below code . <?php $uploaddir = 'images/'; $ran = rand () ; $file = basename($_FILES['userfile']['name']); $uploadfile = $uploaddir .$ran.$file; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "www.host.com/.../images/{$uploadfile}"; } ?>
(OR)
<?php $request_body = @file_get_contents('php://input'); foreach (getallheaders() as $name => $value) { if ($FileName=="FileName") { $header=$value; break; } } $uploadedDir = "directory/"; @mkdir($uploadedDir); file_put_contents($uploadedDir."/".$FileName.".txt", $request_body.PHP_EOL, FILE_APPEND); header('X-PHP-Response-Code: 202', true, 202);
? >
source share