Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:26:21+00:00 2026-06-10T20:26:21+00:00

I tried this code to upload two images to PHP server. But response is

  • 0

I tried this code to upload two images to PHP server. But response is “error”. I want to know, is this method work on iOS 5? Or whats the wrong with my code?

- (IBAction)uploadImage
{    
    NSString *filename= @"After";
    NSString *filename2= @"Before";

    // grab images from documents directory
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString *docDirectory = [sysPaths objectAtIndex:0];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@",docDirectory,@"After.png"];
    NSData *imageData = [[NSData alloc]initWithContentsOfFile:filePath];
    NSString *filePath2 = [NSString stringWithFormat:@"%@/%@",docDirectory,@"Before.png"];
    NSData *imageData2 = [[NSData alloc]initWithContentsOfFile:filePath2];

    // setting up the URL to post to
    NSString *urlString = @"http://uploadimage.php";

    // setting up the request object
    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"];

    // body of the post
    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.png\"\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:@"Content-Disposition: form-data; name=\"image2\"; filename=\"%@.png\"\r\n", filename2] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData2]];

    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    //NSDictionary *response = [[NSDictionary alloc] init];
    //response = [returnString JSONValue];
    NSLog(@"Return string : %@", returnString);
}

If this code is ok, then I can understand error is in the PHP side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:26:22+00:00Added an answer on June 10, 2026 at 8:26 pm

    use ASIHTTPRequest

      -(void)uploadImage
       {
        NSData *imageData = UIImageJPEGRepresentation(imageobj, 90);
        NSURL *url = [NSURL URLWithString:@"url"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setDelegate:self];
        [request setPostValue:@"myImageName" forKey:@"name"];
        [request setData:imageData forKey:@"file"];
    
        [request startAsynchronous];
    }
    
    - (void)requestFinished:(ASIHTTPRequest *)request
    {
        // Use when fetching text data
        NSString *responseString = [request responseString];
        NSLog(@\"response: %@\", responseString);
    
        // Use when fetching binary data
        NSData *responseData = [request responseData];
    }
    
    - (void)requestFailed:(ASIHTTPRequest *)request
    {
        NSError *error = [request error];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have tried this code to redirect a php page.but it s not working
I tried to use this code but it doesn't work. http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html Ld /Users/waitonza/Library/Developer/Xcode/DerivedData/Dr_Ngoo-aanknxmuodcgjicaigxevljxokeq/Build/Products/Debug-iphonesimulator/Dr Ngoo.app/Dr
i am using this code to upload files(images to a folder) <form action='' method='POST'
how to make php can read log file in /var/log/.... I tried this code
i've tried using this code and this to make a random quote generator, but
Hello you naughty code animals.. I have tried to get this code to work,
i have tried this code to upload an image from gallery to sqllite database
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
I am trying to enable mod_deflate. I have Apache 2.0+ and tried this code
I tried with this code to post on the wall (Twitter) of a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.