16

I would like to implement logic that uploads file to FTP Server.

I am new in iPhone development, so want to figure out preferred approach. After browsing available API for this task I have managed to find only code that uses CFNetwork (that looks like not Objective C based).

There is also URL Loading System that uses NSURL etc which are Objective C based.

So, my question: Is it possible to use URL Loading System to implement file upload to FTP Server?

Thanks.

Lyubomyr Dutko
  • 4,486
  • 7
  • 32
  • 39

1 Answers1

32

I use a PHP Page to post a file to and have PHP handle the uploading....

This code is used to upload a photo, but it can be adapted to work with any file.

PHP Code:

<?php
$uploaddir = 'photos/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR";
}

?>

iPhone Code:

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{


    NSString *urlString = @"http://www.yourdomainName.com/yourPHPPage.php";

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

    return ([returnString isEqualToString:@"OK"]);
}

Method Call:

[self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:imageName];
ozking
  • 438
  • 1
  • 5
  • 12
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • I've been attempting to implement your code, but the process always returns an ERROR. I don't fully understand the code block, so it may simply be that the way the data is being implemented is not appropriate for the NSData I'm attempting to send to the php file. What I have is an NSData object which contains the results of an NSKeyedArchiver of an NSMutableArray containing an array of custom objects (in my case database records.) Would you be able to point me to a good resource to understand the code block and/or offer suggestions on how to refine this code for my purpose? – Greg Steiner Jun 04 '12 at 22:27
  • Correction: there was one typo. The code returns OK but the file does not get saved in the directory specified. – Greg Steiner Jun 04 '12 at 22:39
  • Great Man , nice coding – Adnan Abdollah Zaki Nov 28 '15 at 20:14