Uploading to a server

Recently I had to come up with a solution were I pushed files of various types up to a server to be stored. I was able to find a lot of articles on how to consume content from a web service on your iPhone/iPad but very little on how to produce content for a web service to consume. A lot of googling and stackoverflowing lead me to produce this method that coupled with my web service meant that I was able to copy files from my device to the server. The below code is using a HTTP Post to upload the data, similar to how a HTML form works (infact I am using the same backend script to process this POST as I do with the HTML form).

NB: the code below is using files stored with in the application bundle, if you want to use files stored in your Documents folder on the device you will need to change how the path is gathered

-(void)uploadFile:(NSString*)filename withExtension:(NSString*)extension{
	NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:extension];

	NSData *data = [NSData dataWithContentsOfFile:path];

	NSString *urlString = @"http://server/path/to/webservice/call";
	NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	NSString *boundary = @"---------------------------14737809831466499882746641449";
	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	NSMutableData *postbody = [NSMutableData data];
	[postbody appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="uploadedfile"; filename="%@.%@"rn", filename, extension] dataUsingEncoding:NSUTF8StringEncoding]]; //"uploadedfile" should be substituted for whatever variable the backend script expects the file variable to be
	[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
	[postbody appendData:data];
	[postbody appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[request setHTTPBody:postbody];

	NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
	NSLog(@"returned string: %@", returnString); //Just for display purposes


Below is the backend script that processes this POST (this code is from http://www.tizag.com/phpT/fileupload.php and has been slightly modified to match my file structure):

$target_path = "uploaded_files/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";

What do you think? Let me know by getting in touch on Twitter - @wibosco