iPhone image uploading with PHP on the server

This one has been asked before but after googling for hours and trying almost everything I've found, I'm still stuck in the process and desperately hope somebody can shed some light for me.

All I want to achieve is to upload an image from the iPhone to a server running a PHP script.

I've stripped down the iPhone code to this:


NSString *url = @"http://www.domain.com/upload.php";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: url]];
[request setHTTPMethod: @"POST"];
NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"theimage.png"]);
[request addValue:[NSString stringWithFormat:@"%ld", [data length]] forHTTPHeaderField:@"Content-Length"];
[request addValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: data];
NSURLConnection *conn = [NSURLConnection connectionWithRequest: request delegate: self];
[conn retain];


Questions:
How does the server side PHP script retrieve the contents of what I send via setHTTPBody? I am aware that there are $_REQUEST and $_POST global vars but as I don't give a specific field name, I don't know how to grab the image on the PHP side. ($_REQUEST is a named array and expects a named index as in $_REQUEST["lastname"])

Could you please look into this and help? Thanks!

iMac, Mac OS X (10.5.4), iPhone 16GB, iPhone 3G 16GB, Apple Firmware 2.1 | www.treealitygroup.com

Posted on Oct 22, 2008 4:35 PM

Reply
5 replies

Oct 22, 2008 5:33 PM in response to orangekay

Thanks orangekay!

This helps partially. Actually I've got it working now but the Objective-C required to generate the POST request is pretty exhaustive. I wonder wether there is a more elegant way of achieving it with a few lines of code. It currently involves manually crafting every single line of the POST request including "boundary" etc.

Well, but it works.

Question:
Do you happen to know how I could send meta information along with the POST request. As an example if I'd like to send along the name of the photographer of an image, how would I do so.


[body appendData:[[NSString stringWithString:@"Photographer: Mike "] dataUsingEncoding:NSUTF8StringEncoding]];


seems to not work. At least on the server side a PHP $_REQUEST["Photographer"] does return null.

Any idea?

Oct 22, 2008 10:23 PM in response to eknathkadam

Thanks!

Unfortunately it does not work. Here is how the body is constructed as of now - WORKING:


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


I'd like to attach a field "owner" with a value of "peter".

I tried


[body appendData:[[NSString stringWithString:@"owner=peter "] dataUsingEncoding:NSUTF8StringEncoding]];


and


[body appendData:[[NSString stringWithString:@"owner:peter "] dataUsingEncoding:NSUTF8StringEncoding]];


none of which seems to work (at least a PHP


side $_REQUEST["owner"];


does not return owner.

Any more ideas?

Oct 23, 2008 8:18 AM in response to BeSharp

There might be some MIME libraries that you could use to construct the body if you wanted something a bit more elegant. Still, file upload has a pretty basic format. What you have working is probably all you will ever need.

This kind of thing is a 5-minute job in Perl. It is too bad Camelbones is dead. PerlObjCBridge fails once you get anything even mildly complex.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

iPhone image uploading with PHP on the server

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.