title: Unable to Upload Image to Server ios
body: <p>Hello I am trying to upload Image From my IOS device to server.</p>
  
  <p>And Here Is my code to upload the Image</p>
  
  <pre><code>- (IBAction)btnUpload:(id)sender {
  
      if (self.imageViewGallery.image == nil) {
          UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Wait"
                                                               message:@"Please Select An Image To Upload." delegate:nil
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil, nil];
          [ErrorAlert show];
          NSLog(@"error");
      }
      else{
      NSData *imageData = UIImageJPEGRepresentation(self.imageViewGallery.image, 90);
      NSString *urlString = @"http://localhost/ColorPicker/api.upload.php";
  
  
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
      [request setURL:[NSURL URLWithString:urlString]];
  
      [request setHTTPMethod:@"POST"];
      NSString *boundary = @"---------------------------14737809831466499882746641449";
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
      [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
          NSString *imgName = LIbraryImage;
          NSLog(@"image name : %@",imgName);
  
      NSMutableData *body = [NSMutableData data];
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
          [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@\"\r\n", imgName]] 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];
          NSLog(@"setRequest : %@", request);
  
  
      NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
      NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
  
      NSLog(@"returnstring is : %@",returnString);
      }
  </code></pre>
  
  <p>Now here is my server side code. It is a bit long.</p>
  
  <pre><code>  &lt;?php
  
  $Image = $_FILES['files']['name'];
      print_r($_FILES);
  foreach ($Image as $f =&gt; $name) {
  
      $allowedExts = array("gif", "jpeg", "jpg", "png");
      $temp = explode(".", $name);
      $extension = end($temp);
  
      if ((($_FILES['files']['type'][$f] == "image/gif") || ($_FILES['files']['type'][$f] == "image/jpeg") || ($_FILES['files']['type'][$f] == "image/jpg") || ($_FILES['files']['type'][$f] == "image/png")) &amp;&amp; ($_FILES['files']['size'][$f] &lt; 2000000) &amp;&amp; in_array($extension, $allowedExts)) {
          if ($_FILES['files']['error'][$f] &gt; 0) {
              echo "Return Code: " . $_FILES['files']['error'][$f] . "&lt;br&gt;";
          } else {
  
              if (file_exists("upload/" . $name)) {
  
              } else {
                  move_uploaded_file($_FILES['files']['tmp_name'][$f], 'upload/' . uniqid() . "_" . $name);
              }
          }
      } else {
          $error = "Invalid file";
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>Now what happen every time I try to upload got this warning</p>
  
  <pre><code>&lt;b&gt;Warning&lt;/b&gt;:  Invalid argument supplied for foreach() in &lt;b&gt;/Applications/XAMPP/xamppfiles/htdocs/ColorPicker/uploadAction.php&lt;/b&gt; on line &lt;b&gt;5&lt;/b&gt;&lt;br /&gt;
  </code></pre>