Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

What should I do to upload an image from mobile client to PHP server?

HI all,I am developing an application. I am using PHP on server-side. The client side may be iPhone, Android, Blackberry, an ASP.Net website or any platform.

My application has to upload an image and some information to server then download to view later.

Should I parse the image to binary-string before upload it to server? (I mean that I will transfer the image data as I do with a string, an integer,...).Or is there any other better way to do this?

Upvotes: 1

Views: 727

Answers (4)

Benny
Benny

Reputation: 5092

Here is some code I use to send an image over HTTP to a server in an iPhone app, like the previous answer stated, use multipart/form-data via POST.

[EDIT] As Charles mentioned in another answer all variables are accessible through the $_POST array except the image which is available through the $_FILES array, e.g. $_FILES['uploaded'];

- (void)sendImageToServer {

// set up url request
NSURL *url = [NSURL URLWithString:kServerURL];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];

// set content type, needed when sending files
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

// create the body
NSMutableData *body = [NSMutableData data];

// create the POST vars.
NSString *orderType = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"orderType\"\r\n\r\n%@",kTestOrder];
NSString *customerNameString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"customerName\"\r\n\r\n%@",self.customerName.text];
NSString *customerEmailString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"customerEmail\"\r\n\r\n%@",self.email.text];
NSString *customerAddressString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"customerAddress\"\r\n\r\n%@",self.address.text];

NSString *cardTypeString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cardType\"\r\n\r\n%@",@"contact"];
NSString *cardNameString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cardName\"\r\n\r\n%@",self.contactCard.name];
NSString *cardPhoneString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cardPhone\"\r\n\r\n%@",self.contactCard.phone];
NSString *cardEmailString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cardEmail\"\r\n\r\n%@",self.contactCard.email];
NSString *transactionKeyString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"transactionKey\"\r\n\r\n%@",self.transactionKey];


// add the data to POST
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[orderType dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[cardTypeString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[customerNameString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[customerEmailString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[customerAddressString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[cardNameString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[cardPhoneString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[transactionKeyString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[cardEmailString dataUsingEncoding:NSUTF8StringEncoding]];

// add the image
NSString *filename = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploaded\"; filename=\"%@.png\"\r\n",self.email.text];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

NSData *imageData = UIImagePNGRepresentation(self.contactCard.cardImage.imageData);
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// make connection and read returned data
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnedData encoding:NSUTF8StringEncoding];

NSLog(@"Returned from server: %@",returnString);


[returnString release];

}

Upvotes: 1

MLK.DEV
MLK.DEV

Reputation: 453

I've actually been toying around with a script (not completed yet) which I set PHP up to read a specific mail server and to download attachments from emails on that server that match certain criteria (subject, sender, etc...). I can't provide any code, but a lot of what I mentioned people are already doing. Good luck!

Upvotes: 0

Jorge
Jorge

Reputation: 2066

You can just make an http post with multipart form data or, if the images are small you can use base64, send the encoded string and decode it in php.

Upvotes: 0

Charles
Charles

Reputation: 51411

Is there any other better way to do this?

Yes. Use the same upload technique that normal browsers use, RFC 1867's multipart/form-data via POST. The HTTP clients for your various platforms should already have the ability to make compliant requests.

If done correctly, you'll see your file appear in PHP's $_FILES array.

Upvotes: 3

Related Questions