doulian7252 2013-11-20 12:31
浏览 49
已采纳

使用MKNetworkEngine将图像iOS上传到PHP - 我知道它有效,但无法捕获PHP

I created an iOS application which uploads an image taken by the camera to a server using the MKNetworkEngine, as outlined in the following tutorial If I set the host to posttestserver.com, a public POST testing server as the name suggests, it returns a URL and the path to the newly uploaded photo. Success!

Problem: I am unable to use my own PHP code to capture that file and save it on my webserver. I dont understand how posttestserver.com is successful in getting it, but my very straightforward php is not.

iOS side:

NSData *image = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 0.1);

NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                     @"woody", @"userName", @"testPassword", @"password", nil];

 //WORKS:
self.flUploadEngine = [[fileUploadEngine alloc] initWithHostName:@"posttestserver.com" customHeaderFields:dic];
 //Doesnt work:
self.flUploadEngine = [[fileUploadEngine alloc] initWithHostName:@"camerademander.com" customHeaderFields:dic]; 

NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"testApp", @"appID",
                                   nil];
self.flOperation = [self.flUploadEngine postDataToServer:postParams path:@"/users/recieveStub3.php"];
[self.flOperation addData:image forKey:@"userfile" mimeType:@"image/jpg" fileName:@"upload.jpg"];

[self.flOperation addCompletionHandler:^(MKNetworkOperation* operation) {
    NSLog(@"HTTP 200: success");
    NSLog(@"%@", [operation responseString]);
}
                          errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {
                              NSLog(@"%@", error);
                              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                              message:[error localizedDescription]
                                                                             delegate:nil
                                                                    cancelButtonTitle:@"Dismiss"
                                                                    otherButtonTitles:nil];
                              [alert show];
                          }];

[self.flUploadEngine enqueueOperation:self.flOperation];

php:

echo "Connected to server successfully.";
$uploaddir = '/photos/';
$file = basename($_FILES['userfile']['upload.jpg']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['upload.jpg'], $uploadfile)) {
    echo "Successfully uploaded! :{$file}:";
}

This blocks my project from moving forward - I don't understand how posttestserver is able to get a result but I am not, especially when I am able to successfully connect to my own php and server.

Thanks much in advance!

  • 写回答

2条回答 默认 最新

  • dongzhi9457 2013-11-20 14:39
    关注

    In your iOS side,change the key "userfl" to "userfile"

    [self.flOperation addData:image forKey:@"userfile" mimeType:@"image/jpg" fileName:@"upload.jpg"];
    

    In your php side,you should chang your code like this:

    echo "Connected to server successfully.";
    $uploaddir = '/photos/';
    $file = basename($_FILES["userfile"]["name"]);
    $uploadfile = $uploaddir.$file;
    
    if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploadfile)) {
        echo "Successfully uploaded! :{$file}:";
    }
    

    I'm not a PHP Programmer, after look at w3schools's PHP File Upload Tutorials, write the answer, hope it works :)

    UPTATE:

    ok,updated my answer to get more info for your qusetion.(I have test on my server,it works well)

    1. delete the photos dir if exits
    2. use the php code below and see what you get

      if($_FILES){
          $uploaddir = './photos/';
          $file = basename($_FILES["userfile"]["name"]);
          $uploadfile = $uploaddir.$file;
      
          if(!file_exists($uploaddir)){
            if(!mkdirs($uploaddir)){
              echo "Fail to creat uploaddir";
              exit();
            }
          }else if( file_exists($uploadfile)){
             echo "Upload file exists";
             exit();
          }
      
          if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploadfile)) {
              echo "Successfully uploaded! :{$file}:";
          }else{
              echo "Failed uploaded! :{$file}:";
          }
      }else {
          echo "Uploaded empty file";
      }
      
      function mkdirs($dir, $mode = 0777) {
      
          if (is_dir ( $dir ) || @mkdir ( $dir, $mode ))
              return TRUE;
          if (!mkdirs ( dirname ( $dir ), $mode ))
              return FALSE;
          return @mkdir ( $dir, $mode );
      
      }
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)