gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:28:47 25 4
gpt4 key购买 nike

我创建了一个 iOS 应用程序,它使用 MKNetworkEngine 将相机拍摄的图像上传到服务器,as outlined in the following tutorial如果我将主机设置为 posttestserver.com,顾名思义,这是一个公共(public) POST 测试服务器,它会返回一个 URL 和新上传照片的路径。成功!

问题:我无法使用自己的 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 和服务器时。

提前非常感谢!

最佳答案

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

看完 w3schools's PHP File Upload Tutorials 我不是 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 );

    }

关于php - 使用 MKNetworkEngine 将图像 iOS 上传到 PHP - 我知道它有效,但无法让 PHP 捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20096068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com