title: NSURLSessionUploadTask don't upload image with parameters
body: <p>I have this code below which sends an image and some text to my server:</p>
  
  <pre><code> NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
  
      self.session = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: nil];
  
      NSString *requestURL = @"http://www.website.com.br/receive.php?name=StackOverflow";
  
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]];
  
      [request setHTTPMethod:@"POST"];
  
      UIImage *imagem = [UIImage imageNamed:@"Image.jpg"];
  
      NSData *imageData = UIImageJPEGRepresentation(imagem, 1.0);
  
      self.uploadTask = [self.session uploadTaskWithRequest:request fromData:imageData];
  
      [self.uploadTask resume];
  
  
  -(void)URLSession:(NSURLSession *)session
           dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
  
      NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSLog(@"%@",newStr);
  }
  </code></pre>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>&lt;?php
  echo $_POST['name'];
  ?&gt;
  </code></pre>
  
  <p>The problem with this code is that the <code>didReceiveData</code> method does not receive the data going to the server, it only gets an <code>NSData</code> when I put this code in php file: </p>
  
  <pre><code>print_r($_FILES);
  </code></pre>
  
  <p>And yet it returns an empty array, why this is happening?</p>
  
  <p><strong>Solved</strong></p>
  
  <p>Well, I solved my problem, lets go, In .h file you need to implement this protocols and one property:</p>
  
  <pre><code>&lt; NSURLSessionDelegate, NSURLSessionTaskDelegate&gt;
  @property (nonatomic) NSURLSessionUploadTask *uploadTask;
  </code></pre>
  
  <p>whereas in .m file there is a method of IBAction type and that it is connected to a particular button existing in our view, we need only do this:</p>
  
  <pre><code>- (IBAction)start:(id)sender {
  
      if (self.uploadTask) {
          NSLog(@"Wait for this process finish!");
          return;
      }
  
     NSString *imagepath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"myImage.jpg"];
      NSURL *outputFileURL = [NSURL fileURLWithPath:imagepath];
  
  
      // Define the Paths
      NSURL *icyURL = [NSURL URLWithString:@"http://www.website.com/upload.php"];
  
      // Create the Request
      NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:icyURL];
      [request setHTTPMethod:@"POST"];
  
      // Configure the NSURL Session
      NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.sometihng.upload"];
  
      NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
  
      // Define the Upload task
      self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:outputFileURL];
  
      // Run it!
      [self.uploadTask resume];
  
  }
  </code></pre>
  
  <p>And implement some delegates methods:</p>
  
  <pre><code>- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  
      NSLog(@"didSendBodyData: %lld, totalBytesSent: %lld, totalBytesExpectedToSend: %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
  
  }
  
  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { 
      if (error == nil) {
          NSLog(@"Task: %@ upload complete", task);
      } else {
          NSLog(@"Task: %@ upload with error: %@", task, [error localizedDescription]);
      }
  }
  </code></pre>
  
  <p>And for finish, you need create a PHP file with this code:</p>
  
  <pre><code>&lt;?php
  
  $fp = fopen("myImage.jpg", "a");//If image come is .png put myImage.png, is the file come is .mp4 put myImage.mp4, if .pdf myImage.pdf, if .json myImage.json ...
  
  $run = fwrite($fp, file_get_contents("php://input"));
  
  fclose($fp);
  
  ?&gt;
  </code></pre>