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 8837617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:46:20+00:00 2026-06-14T09:46:20+00:00

I have searched a lot for a working uploading code using AFNetwork but no

  • 0

I have searched a lot for a working uploading code using AFNetwork but no luck. I came up with this code which I got it from 2 different sites:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]);
NSURL *url = [NSURL URLWithString:@"http://localhost/project"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"google" fileName:@"1.png" mimeType:@"image/png"];
}];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
} failure:nil];

[operation start];

But what I get is “IP Address: (null)”. My php side is this:

function upload(){
$uploaddir = 'uploads/';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    sendResponse(200, 'Upload Successful');
    return true;
}
sendResponse(403, 'Upload Failed');
    return false;
}

I really appreciate a little help.

  • 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-14T09:46:22+00:00Added an answer on June 14, 2026 at 9:46 am

    I’ve made this just some days ago. Code on the iPhone side (it uploads an image and another text parameter, item)

    NSString *uploadURL = @"http://www.baseurl.it/";
    NSURL *url = [[NSURL alloc]initWithString:uploadURL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
    NSDictionary *dict = [NSDictionary
                          dictionaryWithObjects:@[self.itemValue]
                          forKeys:@[@"item"]];
    // self.itemValue is a post parameter type nsstring
    
    NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"path/to/page.php" parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
    // self.itemData is an NSData containing the image
        if (self.itemData)
            [formData appendPartWithFileData:self.itemData name:@"imageData" fileName:@"imageData.jpg" mimeType:@"image/png"];
    }];
    
    // sorry for the formatting here, is a long method using blocks
    AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                   success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                       NSLog(@"Upload Success");
                                                                                   }                                                                                   
                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                       NSLog(@"Error: %@", error.description);
    
                                                                                   }];
    [json start];
    

    On the Php side:

    // file upload
    try {
    $fileName = $_FILES['imageData']['name'];
    $tmpName  = $_FILES['imageData']['tmp_name'];
    $fileSize = $_FILES['imageData']['size'];
    $fileType = $_FILES['imageData']['type'];
    
    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);
    
    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
    }
    
    } catch (Exception $e) {
        echo json_encode("Error:".$e);
    }
    
    // $content contains your image
    

    Not sure if it’s error free, because it has been taken from a longer source, so I’ve modified some pieces of code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched a lot, but no any solutions are working for me. I
I am sorry, if this question sounds stupid, but I have searched a lot
I have searched a lot about the difference between Website and WebApplication but didn't
I have searched a lot and tried much but I can not find the
I have searched and found a lot of different things but none that actually
I have searched for a general solution to this but only find answers to
I'm using this JSON framework . It's working well but I would like to
This might sound like a duplicate, but I have searched through the forum questions
I've searched a lot after a solution for this, but the only thing that
My question is self explanatory. I have searched a lot but could not found

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.