Uploading image to server in iphone application

I want upload image to sever in iphone app I am using following code for PHP

but using this code i get error displayed on page when i run localhost/upload.php;

  • Why is my library not able to expand on the CocoaPods website?
  • Swift - Type '*' does not conform to protocol '*'
  • Hiding properties from public framework headers, but leaving available internally
  • UISlider with increments of 5
  • What does @“some string” mean in objective-c?
  • How to change inside background color of UISearchBar component on iOS
  •   <?php
      $uploaddir = 'photos/';
      $file = basename($_FILES['userfile']['name']);
      $uploadfile = $uploaddir . $file;
    
     if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "OK";
     else {
     echo "ERROR";
     }
    
      ?>
    

    Below is the iphone xcode code

        - (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{
    
    
          NSString *urlString = @"http://localhost/upload.php";
    
          NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
         [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:@"\r\n--%@\r\n",boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content- Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"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] autorelease];
    
        return ([returnString isEqualToString:@"OK"]);
          }
    

    Calling method

         [self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:@"pig.jpg"];
    

    2 Solutions Collect From Internet About “Uploading image to server in iphone application”

    if you think error in php code then first try this print_r($_FILES); and see what did you get in the $_FILES['userfile']

    try this and also did you check in php by print_r($_FILES);

                        NSString *urlString = @"http://localhost/upload.php";
    
                        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
                        [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:@"\r\n--%@\r\n",boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
                        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
                        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                        [body appendData: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] autorelease];