为什么NSURLSession uploadTaskWithRequest:fromData:无法上载到php服务器?

为什么NSURLSession uploadTaskWithRequest:fromData:无法上载到php服务器?,php,ios,nsurlsessionuploadtask,Php,Ios,Nsurlsessionuploadtask,php代码运行良好。我从同一台服务器上的html表单上传了文件。上传的文件从40K到2.0M不等,所以大小不一样。文件上传在运行PHP5.3的服务器上激活 我发现了很多像这样的帖子(还没有答案): 这一个使用uploadTaskWithRequest:fromFile:而不是fromData: 这是NSURLSessionDataTask,而不是uploadTaskWithRequest: 有些帖子似乎说uploadTaskWithRequest:fromData:根本不起作用: 和 我已经做

php代码运行良好。我从同一台服务器上的html表单上传了文件。上传的文件从40K到2.0M不等,所以大小不一样。文件上传在运行PHP5.3的服务器上激活

我发现了很多像这样的帖子(还没有答案):

这一个使用uploadTaskWithRequest:fromFile:而不是fromData:

这是NSURLSessionDataTask,而不是uploadTaskWithRequest:

有些帖子似乎说uploadTaskWithRequest:fromData:根本不起作用: 和

我已经做了这样的应用程序,它返回HTTP代码,我得到代码200。上传后,服务器错误日志文件中没有任何内容。看起来一切正常,但不管发生什么情况,文件都不会写入服务器。你知道我还能找出什么问题吗

以下是php代码:

<?php
$file='';
$uploaddir='';

////////////////
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
////////////////

if(isset($_FILES['userfile']['name'])){
      $uploaddir = './photos/'; //Uploading to same directory as PHP file
      $file = basename($_FILES['userfile']['name']);
      echo "file is set";
      $uploadFile = $file;
      $randomNumber = rand(0, 99999); 
      $newName = $uploaddir . $uploadFile;
        if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
            echo "Temp file uploaded. \r\n";
        } else {
            echo "Temp file not uploaded. \r\n";
        }
   if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
         $postsize = ini_get('post_max_size'); //Not necessary, I was using these
         $canupload = ini_get('file_uploads'); //server variables to see what was 
         $tempdir = ini_get('upload_tmp_dir'); //going wrong.
         $maxsize = ini_get('upload_max_filesize');
         echo "\r\n" .  $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    }
}
?>
我对该文件夹有777个权限。正如我所说,我能够通过html表单从同一个php脚本上传并写入该文件夹

http错误代码的totalResponse为200,count=0,php转储的数组(0){}

以下是应用程序中服务器响应的图像:

我在回显上传的临时文件后添加了以下行:

$body = $HTTP_RAW_POST_DATA;
echo "\n REQUEST" . $body;
但我只得到了这个:

file count=1 array(1) { ["user file"]=> array(5) { ["name"]=> string(12) "DrinkCO2.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/tmp/phpxg7oLZ" ["error"]=> int(0) ["size"]=> int(190550) } } file is setTemp file uploaded. REQUEST 190550 image/png

我在这里遇到了分号问题->

"name=\"userfile\" ; filename=\"%@\"\r\n"" 

所以它基本上是一个畸形的身体。

它没有缺少一个冒号,它缺少一个分号

"name=\"userfile\" : filename=\"%@\"\r\n""
应该是

"name=\"userfile\"; filename=\"%@\"\r\n""

至少这是您的代码为我工作的唯一方式(顺便说一句,谢谢!)。

您是否查看过使用Wireshark或Charles Proxy之类的工具发送/接收的流量?在我看来,您从iOS设备发出的请求某种程度上是格式错误的(例如,边界错误),你能发布浏览器表单生成的Http请求+正文吗?为了清晰起见,我在底部的原始问题中添加了php行和帖子的结果。这是iOS请求的结果还是Web表单的结果?Web表单提交到同一php脚本的结果。
"name=\"userfile\"; filename=\"%@\"\r\n""