dongqing5925 2014-10-07 10:34
浏览 954
已采纳

在PHP中读取multipart / form-data POST数据

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:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"
", @"teacherimage"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:Array.picture]];

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

%@", postSTRING] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"
--%@
", 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

  • 写回答

1条回答 默认 最新

  • dsk95913 2014-10-07 10:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!