0
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
 misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
[email protected] and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
<p>Additionally, a 500 Internal Server Error
error was encountered while trying to use an ErrorDocument to handle the request.</p>
 <hr>
<address>Apache Server at domain.com Port 80</address>
 </body></html>

iOS developers can tried to send image to PHP server. But they are getting an error like above

How can i resolve this issue?

is it in iOS code issue or PHP side issue?

through postman i checked: enter image description here

by using this URL they are sending me multiple images but if it is single image it's working fine but trying to send multiple images it's not working: https://charangiri.wordpress.com/2014/09/22/how-to-upload-multiple-image-to-server/

12
  • It could be on both sides. Please check the API URL they are using is correct. Firstly the API you have created check it on Postman yourself or use a curl request to check it at your end. If you are able to upload the picture on the server, then your API is perfect. The iOS developers are making some mistake. Commented Aug 9, 2016 at 11:25
  • I posted POSTMAN screenshot. Please check it out once: Commented Aug 9, 2016 at 11:34
  • by using this URL they are sending me multiple images but if it is single image it's working fine but trying to send multiple images it's not working: charangiri.wordpress.com/2014/09/22/… Commented Aug 9, 2016 at 11:35
  • Are they creating the request as multi-part form data ? Commented Aug 9, 2016 at 11:37
  • Yes @Jassi, They are trying to sending multi-part form data only. ---> single image it's working fine but not in multiple images purpose Commented Aug 9, 2016 at 11:40

1 Answer 1

1

You can use this function below to upload multiple images to server. The imagesarray variable I have used is to store UIImage objects. You can change it as per your need.

- (void)uploadImagesToServer{
//create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

//Create boundary, it can be anything
NSString *boundary = @"------friendlyWagonBoundary4QuqLuM1cE5lMwCy";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

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

for (int i=0; i<[imagesArray count]; i++)
{
    UIImage *images=[imagesArray objectAtIndex:i];
    NSData* imageData = UIImageJPEGRepresentation(images, 0.7);

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image%d\"; filename=\"image%d\"\r\n", i+1,i+1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

}
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

// set URL
[request setURL:[NSURL URLWithString:@"http://example.com"]]; // Give your URL here.

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                           NSString *str = [NSString stringWithUTF8String:[data bytes]];
                           NSLog(@"str %@",str);
                           if ([httpResponse statusCode] == 200) {

                               NSLog(@"success");
                           }else{
                               NSLog(@"failed");
                           }

                       }];
}

Hope this will help you!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.