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

在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.

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失