How to upload image on wordpress server by webservice with new “uploads” directory in iphone programming?

I need to upload image by a webservice which is of wordpress server ..

Image is not uploaded because of wordpress structure ..

Read More

This is my basic code for image upload in iPhone ..

NSData *myData=UIImagePNGRepresentation([self.img image]);
NSMutableURLRequest *request;
NSString *urlString = @"http://xyzabc.com/iphone/upload.php";
NSString *filename = @"filename";
request= [[[NSMutableURLRequest alloc] init] autorelease];
[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"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="%@.jpg"rn", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:myData]];
[postbody appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString;
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);

It returns whole page in returnString !!!

I need only one response is a path of image that I uploaded..

Any one have idea of that?

Related posts

Leave a Reply

1 comment

  1. I am a wordpress developer. You can use this code in your web service.

    if (!function_exists('wp_generate_attachment_metadata')){
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }
    $overrides = array( 'test_form' => false);
    $file = wp_handle_upload($_FILES['image_url'], $overrides);
    $file['url'];
    
    $content = $file['url'];