Display Name
Display Name

Reputation: 1035

Reading multipart/form-data POST data in PHP

I am developing an app that sends data to a webservice. The data transfered uses https messages with POST method.

On the server side, in PHP i wrote code that takes data from the POST message (as long as data is a string),

by using technique as follows.

$servercall = $_POST['servercall'];

Issue i have now found is how do i read data when i transmit multipart/form-data.

In objective-c i construct the http message as follows.

NSURL *url = [NSURL URLWithString:@"<ADDRESS>"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"gc0p4Jq0M2Yt08jU534c0p";
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=\"photo\"; filename=\"%@.jpg\"\r\n", @"teacherimage"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:Array.picture]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", postSTRING] dataUsingEncoding:NSUTF8StringEncoding]];

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

[request setHTTPBody:body];

how do i read the photo and textstring in the http message in PHP? I dont even know the technique for me to google.

If it makes any difference all data is being saved in SQL database.

Thanks

Upvotes: 0

Views: 471

Answers (1)

I don't know Objective-C but php has the $_FILES array, where you can find files through forms. Sorry if my answer doesn't make sense, I never used objective-c and not planning to use it. Try to use var_dump($_FILES). You don't need to define/declare it, because it's a php superglobal.

Upvotes: 1

Related Questions