send a zip file from the ios application to the php server

advertisements

I am building a client-server app in which the app is sending a zip file that contains a lot of images and the php server should check if the file was received and returns the number of the images in the zip file that was sent.

here is my code in the app:

        NSData *zipData = [[NSData alloc] initWithContentsOfFile:zipFile]; // zipFile contains the zip file path

    NSString *postLength = [NSString stringWithFormat:@"%d", [zipData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://localhost/test.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:zipData];

    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %d", [urlResponse statusCode]);

    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
    {
        NSLog(@"Response: %@", result);
    }

and here is my php file that runs on local host using xampp:

<?php
print_r($_POST);

print_r($_FILES);
?>

the zip file is attached to the request using sethttpbody, I am trying to view the file in the php as shown above but it is always empty, please help me with that , moreover is that right to see the sent zip file by printing the $_POST and $_FILES if not what is the right way to do that, the zip file is converted to NSDATA, also I have checked that the connection is established but I am really confused with this.


While I wouldn't recommend it (I'd prefer to see a well-formed multipart/form-data or application/JSON request), you could create your request like so:

NSData *zipData = [NSData dataWithContentsOfFile:zipFile]; // note, autorelease object

NSString *postLength = [NSString stringWithFormat:@"%d", [zipData length]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/zip" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:zipData];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

    NSInteger statusCode = -1;
    if ([response isKindOfClass:[NSHTTPURLResponse class]])
        statusCode = [(NSHTTPURLResponse *)response statusCode];

    NSLog(@"Response Code: %d", statusCode);

    if (statusCode >= 200 && statusCode < 300)
    {
        NSLog(@"Response: %@", result);
    }
}];

And then you could have PHP code that reads this binary data and saves it to a file:

<?php

$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
    $http_raw_post_data .= fread($handle, 8192);
}
fclose($handle);

$handle = fopen('upload/myzip.zip', 'wb');

if ($handle == false) {
    echo 'unable to open file';
    exit (0);
}

$bytes = fwrite($handle, $http_raw_post_data);

if ($bytes == false)
    echo 'write failed';
else
    echo 'write succeeded';

fclose($handle);

?>