【问题标题】:File upload problem in iphoneiphone文件上传问题
【发布时间】:2011-06-03 08:05:20
【问题描述】:

我想从我的 iphone 应用程序上传一个 plist 文件到我的服务器。我已经尝试了以下代码(发现谷歌搜索),但我的文件仍然没有上传。问题出在哪里?

iphone 应用代码(处理上传的方法):

- (IBAction)sendHTTPPost:(id)sender {

    NSString *path = [self pathOfFile];
    NSString *fileName = @"Contacts";
    NSData *data = [[NSData alloc] initWithContentsOfFile:path]; 
    NSString *urlString = @"http://localhost/ajaxim/fileUpload.php";

    //set up request
    NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    //required xtra info
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    //body of the post
    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=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];

    //lets make the connection
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    returnString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //NSLog(returnString);
    }

服务器的php代码:

<?php
$target = "./upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
  echo "YES";
  echo "http://localhost/ajaxim/upload/{$target}";
}
else {
   echo "Not uploaded";
}
?> 

请给我一些建议,我应该在哪里更改代码或我哪里错了?

【问题讨论】:

    标签: php iphone file-upload


    【解决方案1】:

    在我看来,您将文件以userfile 发布,并尝试在服务器端以uploaded 读取它。

    制作这件制服,它应该可以工作。

    【讨论】:

    • 你能检查你的$_POST$_FILES 数组吗?这能提供线索吗?
    【解决方案2】:

    我知道这有点晚了:

    我没有看到您在哪里向我们提供了您使用的服务器类型。

    如果使用 unix 或 linux 服务器。包括我自己在内的很多人都忘记检查您正在使用的目录的权限。 您要确保您拥有添加、移动和删除文件的适当权限。

    这可以通过使用 shell 导航到包含上传文件夹的目录并运行以下命令来安全地完成:

    chown -R 没有人你的目录名
    chmod -R 755 你的目录名

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 2010-10-17
      相关资源
      最近更新 更多