用php从iPhone应用程序上载数据到web服务器


upload data from an iphone app to a web server with php

在PHP几乎没有任何知识,我试图发送一些数据(NSData)从我的iphone应用程序到我的web服务器(本地现在),但没有成功(PHP上传程序脚本被调用,因为我得到它的错误信息)。有没有人能帮我理解我在这里做错了什么?

我的iphone应用程序在这一点上只包括以下代码在我的(唯一的)viewController的ViewDidLoad,这是我从这里的一些对话组合:

- (void)viewDidLoad
{
[super viewDidLoad];
  NSData *data;    
const char *bytestring = "black dog";
data = [NSMutableData dataWithBytes:bytestring length:strlen(bytestring)];
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSString *urlString = @"http://localhost/uploader1.php";
NSString *filename = @"filename";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"'r'n--%@'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name='"userfile'"; filename='"%@.jpg'"'r'n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream'r'n'r'n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data]];
[postbody appendData:[[NSString stringWithFormat:@"'r'n--%@--'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}

我还尝试了以下PHP上传脚本:

  <?php
    $target = "./upload/";
        $target = $target.basename( $_FILES['userfile']['name']) ;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "SUCCESS";
}
?> 

我做错了什么,为什么我不能发布这个数据?再次,PHP脚本被调用因为我得到错误信息:错误的文件类型…

任何帮助都将非常感激。

谢谢!

在你的Objective-C代码中,你是这样发送文件的:

 name='"userfile'"; filename='"%@.jpg'"'r'n",

因此,在PHP端,您应该访问$_FILES["userfile"],而不是$_FILE["file"]$_FILE["uploaded"],以获得有关您上传的正确信息。

如果您仍然遇到问题,第一个建议是在服务器端设置一些跟踪:

print_r($_REQUEST);

将为您提供所有$_POST/$_GET/cookies输入参数的列表,因此您可以更好地了解正在发生的事情。此外,我建议还跟踪:

print_r($_FILES);

可能是ObjC端发送了错误的数据,或者是服务器端读取了错误的数据。

编辑:你得到的错误:

"Wrong file type..."

取决于您试图计算上传文件的大小,就好像它是一张图像一样:

 $check = getimagesize($_FILES['userfile']['tmp_name']);
 if($check[0]>0 && $check[1]>0 && $check['mime']=='image/jpg'){

,而实际上你正在发送一个简单的字符串作为文件内容:

 const char *bytestring = "black dog";

你要么删除支票,要么上传一个真正的文件。

编辑:

使用这些行:

error_reporting(E_ALL);
ini_set("display_errors", 1); 

使错误报告更详细,因此您可能知道为什么失败…

您是否尝试过ASIHTTPRequest?http://allseeing-i.com/ASIHTTPRequest/

如果目标路径不使用HTTP,则使用下面的方法。

代替file_put_contents(***WebSiteURL***...),你需要使用/cache/lang/file.php的服务器路径(例如/home/content/site/folders/filename.php)。

您不能通过HTTP打开文件并期望它被写入。相反,您需要使用本地路径打开它。