title: Uploading an image from iOS to PHP
body: <p>I've been working on uploading an UIImage to an webserver. The problem is, that the image never appears on the Server. A normal POST from an HTML file works. I think it's an issue in my objective-c code.</p>
  
  <p>Here is my objective-c code:</p>
  
  <pre><code>NSData *imageData = UIImagePNGRepresentation(delegate.dataBean.image);
  NSString *urlString = [NSString stringWithFormat:@"%@test.php", delegate.dataBean.hosterURL];
  
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  [request setURL:[NSURL URLWithString:urlString]];
  [request setHTTPMethod:@"POST"];
  
  NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  
  NSMutableData *body = [NSMutableData data];
  [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[NSData dataWithData:imageData]];
  [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [request setHTTPBody:body];
  
  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
  
  NSLog([NSString stringWithFormat:@"Image Return String: %@", returnString]);
  </code></pre>
  
  <p>Here is my PHP code:</p>
  
  <pre><code>&lt;?php
  $uploaddir = 'uploads/';
  $file = basename($_FILES['uploadedfile']['name']);
  $uploadfile = $uploaddir . $file;
  
  echo "file=".$file; //is empty, but shouldn't
  
  if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
      echo $file;
  }
  else {
      echo "error";
  }
  ?&gt;
  </code></pre>