【问题标题】:Uploading an image to php server from iphone将图像从 iphone 上传到 php 服务器
【发布时间】:2013-12-18 17:34:46
【问题描述】:

我正在尝试将图像上传到服务器

以下是iphone和php代码

    // create request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:30];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"0xKhTmLbOuNdArY";
    NSString *url = [NSString stringWithFormat:POST_URL,BASE_URL, [Utilities myUserId],@"", TYPE_IMAGE];

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:self.chatBox.text forKey:@"comment"];

    // add params (all params are strings)
    for (NSString *param in params)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    if (picData)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"uploadedfile"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:picData];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set URL
    [request setURL:[NSURL URLWithString:url]];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection)
    {
        [self dismissKeyboard];
    }

PHP代码是

   $directoryPath = "userId_" . $userId;
    $this->makeDir("uploads/images/" . $directoryPath);
    $this->makeDir("uploads/thumbs/" . $directoryPath);

    $val = $userId .'_' . time() . ".jpg";
    $mainPath  = "uploads/images/" . "image_" . $val;
    $thumbPath = "uploads/thumbs/" . "thumb_" . $val;

    if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $mainPath))
        return true;
    else
        return false;


    if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $mainPath))

返回 false。有什么问题。

  ["userfile"]=>
  array(5) {
    ["name"]=>
    string(6) "dr.jpg"
    ["type"]=>
    string(0) ""
    ["tmp_name"]=>
    string(0) ""
    ["error"]=>
    int(1)
    ["size"]=>
    int(0)
  }

【问题讨论】:

  • 目录上传/图像和上传/拇指是否正在制作?您有权这样做吗?
  • 它们已经制作好了,我已经设置了权限 777。我不知道还有什么问题,我已经测试过,$file = basename($_FILES['userfile']['姓名']);给出正确的名称,所以上传是好的
  • 如果你在 if 语句之前 var_dump($_FILES),输出是什么?
  • @user602525 检查修改后的代码

标签: php iphone objective-c nsmutableurlrequest nsmutabledata


【解决方案1】:

您提交的文件大小超过了您在 php.ini 中设置的 upload_max_filesize,这可以通过您的 var_dump() 并检查错误键来确定,在这种情况下 = 1

Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

来自文档 - 默认 upload_max_filesize 为 2MB

http://www.php.net/manual/en/features.file-upload.errors.php

常见的陷阱:

http://www.php.net/manual/en/features.file-upload.common-pitfalls.php

【讨论】:

  • 我已经编辑了 php.ini 并将默认值更改为 200M,但结果是一样的。错误仍然相同:(
  • 错误键仍然是 int(1) ?您尝试上传的文件有多大?
  • 2697777 字节,也许我需要重新启动 apache?
  • 是的,一旦你改变了 ini 你必须重启 apache
猜你喜欢
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 2011-02-02
  • 2015-03-29
  • 2011-10-15
  • 2010-12-28
相关资源
最近更新 更多