0

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:@"--%@\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 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)
  }
3
  • Is directory uploads/images and uploads/thumbs getting made? Do you have permission to do so? Commented Dec 18, 2013 at 17:53
  • they are already made, i have already set Permissions 777. I don't know what else is the isse, i have tested, the $file = basename($_FILES['userfile']['name']); gives correct name so upload is ok Commented Dec 18, 2013 at 17:58
  • if you var_dump($_FILES) before the if statement, whats the output? Commented Dec 18, 2013 at 18:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I have edited the php.ini and changed the default val to 200M but the result is same. error is still same :(
the error key is still int(1) ? How large is the file you are trying to upload?
2697777 bytes, mabye i need to restart apache?
yea once you change ini you gotta restart apache

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.