0

我创建了一个iOS应用程序,它使用 将相机拍摄的图像上传到服务器MKNetworkEngine如以下教程中所述新上传照片的路径。成功!

问题:我无法使用我自己的 PHP 代码来捕获该文件并将其保存在我的网络服务器上。我不明白posttestserver.com是如何成功获得它的,但我非常简单的 php 不是。

iOS端:

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}:";
}

这阻止了我的项目向前发展——我不明白 posttestserver 是如何获得结果的,但我不是,尤其是当我能够成功连接到我自己的 php 和服务器时。

提前非常感谢!

4

2 回答 2

1

在您的 iOS 端,将密钥“userfl”更改为“userfile”

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

在您的 php 方面,您应该像这样更改代码:

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}:";
}

我不是PHP程序员,看了w3schools的PHP文件上传教程,写下答案,希望有用:)

更新:

好的,更新了我的答案以获取有关您的问题的更多信息。(我在我的服务器上进行了测试,效果很好)

  1. 如果退出,删除照片目录
  2. 使用下面的 php 代码,看看你得到了什么

    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 );
    
    }
    
于 2013-11-20T14:39:52.163 回答
0

原来这是一个服务器权限问题 - 我曾经echo $file, is_writable($file) ? ' is writable' : ' is NOT readable', "\n";确定这一点。

于 2013-11-24T13:11:53.807 回答