【问题标题】:iPhone/Cocoa Upload Image File to PHP on Ubuntu (LAMP)iPhone/Cocoa 在 Ubuntu (LAMP) 上将图像文件上传到 PHP
【发布时间】:2011-11-14 22:21:33
【问题描述】:

我正在尝试将一个小的 .png 文件(50x50 像素)从我的 iPhone 应用程序上传到我的 ubuntu 服务器。我已经尝试了几种方法来解决在 StackOverflow 和网络上其他地方发布的问题,但还没有成功。我已经发布了我的 Obj-C 代码来上传文件并记录服务器响应、服务器上的 PHP 代码以及我得到的响应。如果您发现我的代码有任何问题,请告诉我 - 谢谢!

Obj-C 代码:

// This method uploads the image to the server
-(void)uploadImage{

    NSURL *url = [NSURL URLWithString:@"http://mydomain.com/upload.php"];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    [request setUseKeychainPersistence:YES];   

    NSString *fileName = [NSString stringWithFormat:@"%@",self.avatarFileName];
    [request addPostValue:fileName forKey:@"name"];

    // Upload an image
    NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:self.avatarFileName]);

    // Make sure it's not null..
    NSLog(@"%d",[imageData length]);

    // Set the data and filename
    [request setData:imageData withFileName:fileName andContentType:@"image/png" forKey:@"userfile"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(uploadRequestFinished:)];
    [request setDidFailSelector:@selector(uploadRequestFailed:)];

    [request startAsynchronous];
}

- (void)uploadRequestFinished:(ASIHTTPRequest *)request{    
    NSString *responseString = [request responseString];
    NSLog(@"Upload response %@", responseString);
}

- (void)uploadRequestFailed:(ASIHTTPRequest *)request{

    NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]); 
}

Ubuntu 服务器上的 PHP 代码:

<?php
echo ' -- Hit Script --';
print_r($_FILES);
$target_path = "files/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There w
as an error uploading the file, please try again!"; }
?>

来自服务器的响应:

Upload response  -- Hit Script --
Array
(
)
There was an error uploading the file, please try again!

【问题讨论】:

    标签: php iphone cocoa-touch image upload


    【解决方案1】:

    我认为您使用密钥用户文件上传文件,而不是上传文件(使用 $_FILES['userfile'])。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2015-01-06
      • 2021-09-29
      • 1970-01-01
      相关资源
      最近更新 更多