doudeng2057 2013-12-18 17:34
浏览 86
已采纳

从iphone上传图像到php服务器

I am trying to upload an image to server

following is iphone and php code

    // 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:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"

", param] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@
", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
    }

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

" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:picData];
        [body appendData:[[NSString stringWithFormat:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"--%@--
", 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 Code is

   $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))

is returning false. what can be wrong.

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

1条回答 默认 最新

  • dougaxing8673 2013-12-18 18:10
    关注

    The file size you are submitting exceeds the upload_max_filesize set in your php.ini, this can be determined by your var_dump() and examining the error key, which in this case = 1

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

    From the docs - the default upload_max_filesize is 2MB

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

    Common pitfalls:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?