【问题标题】:Why keeps my $_FILES empty when I post data in objective-c to server?为什么当我将 Objective-c 中的数据发布到服务器时,我的 $_FILES 为空?
【发布时间】:2014-12-29 16:31:47
【问题描述】:

我正在尝试将一段文本发布到服务器,但到目前为止没有任何运气。 我所做的是使用下面的代码将字符串 POST 到服务器。 为了测试服务器是否正在获取任何数据,我使用以下命令:

<?php
     print_r($_FILES);
?>

发布字符串:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://[webaddress]/[page].php"]];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:30];
    [request setHTTPMethod:@"POST"];

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

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"test"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n",@"testvalue"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
           [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
           NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:body];
    connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    [connection start];

在我的测试环境中,我使用真实地址,但出于安全考虑,我将其删除。 我使用 NSLog 来返回服务器的结果(在 NSURLConnection 委托中使用)。 但我的问题是 $_FILES 一直空着? 奇怪的是,当将以下内容与图像一起使用时,它确实给了我一些回报:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://[webaddress]/[page].php"]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    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=\"userfile\"; filename=\"%@\"\r\n",name] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:data]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the request

    [request setHTTPBody:body];
     connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    [connection start];

所以要上传图片(最后一段代码),$_FILES 不为空,而在使用第一段代码时它为空。 服务器工作正常,用于返回 $_FILES 的文件只是一个普通的 PHP 文件。 PHP 正在运行,并且可以上传图像这一事实让我认为第一段代码存在错误,而不是服务器。

所以我的问题是为什么当我上传普通字符串时 $_FILES 是空的?

【问题讨论】:

  • 顺便说一下,除非您使用将startImmediately 设置为NO 的再现,否则不要使用initWithRequest 调用start。实际上,您正在启动连接两次。删除对start 的调用。有关更多信息,请参阅start 方法的文档。
  • 没错,我很沮丧,因为我没有从服务器得到一个好的答案。所以我尝试显式启动连接,但我应该将 init 设置为 NO。

标签: php objective-c post


【解决方案1】:

文件数据/信息将在$_FILES 中,发布的表单数据(字符串)将在$_POST 中,或者如果您使用get 方法,数据将在$_GET 中。

【讨论】:

  • 有人可以打我吗!上传图片时您是绝对正确的,因此 $_FILES 不会为空,而仅上传文本时它将位于 $_POST 中。谢谢,回答了另一个菜鸟问题;)
猜你喜欢
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多