Search code examples
phpobjective-cimage-uploading

PHP empty TMP_NAME on iOS Upload Image


I am currently developing an iOS project that requests the user to upload an image to the server.

I currently have this code in my class in Objective-C:

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setHTTPShouldHandleCookies:NO];
        [request setTimeoutInterval:60];
        [request setHTTPMethod:@"POST"];
        NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setValue:[SSKeychain passwordForService:@"ID" account:@"SpotterBike"] forKey:@"ID"];

        for (NSString *param in parameters) {
            [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", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
        }

        NSString *FileParamConstant = @"uploadedfile";
        NSData *imageData = UIImageJPEGRepresentation(image, 1);

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

        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];
        [request setURL:[NSURL URLWithString:@"http://212.92.57.155/App/UploadImage.php"]];

However, although the server recieves the image, I am not able to process it for further usage in it. As you see, tmp_name is empty, so I am not able to move the image to the directory.

print_r($_FILES['uploadedfile']);

(
    [name] => image.jpg
    [type] => 
    [tmp_name] => 
    [error] => 1
    [size] => 0
)

Any idea of why this is happening?


Solution

  • Just in case someone is facing the same problem, after some days of research, I have found the problem.

    The images were uploaded with 3G mobile connections, as a result, there was certain amount of delay in beetwen the time the upload started until it finished.

    Due the fact that, the MAX_REQUEST_TIMEOUT property had a low value, the image was not able to reach the server before the MAX_REQUEST_TIMEOUT exceeded, so the upload was aborted.

    Hope it helps!