Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading an image from iOS to PHP

Tags:

I've been working on uploading an UIImage to an webserver. The problem is, that the image never appears on the Server. A normal POST from an HTML file works. I think it's an issue in my objective-c code.

Here is my objective-c code:

NSData *imageData = UIImagePNGRepresentation(delegate.dataBean.image); NSString *urlString = [NSString stringWithFormat:@"%@test.php", delegate.dataBean.hosterURL];  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [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:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"rn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body];  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];  NSLog([NSString stringWithFormat:@"Image Return String: %@", returnString]); 

Here is my PHP code:

<?php $uploaddir = 'uploads/'; $file = basename($_FILES['uploadedfile']['name']); $uploadfile = $uploaddir . $file;  echo "file=".$file; //is empty, but shouldn't  if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {     echo $file; } else {     echo "error"; } ?> 
like image 561
Michael Horner Avatar asked Jul 10 '12 12:07

Michael Horner


People also ask

How to upload photo in registration form in PHP?

basename( $_FILES['photo']['name']); //This gets all the other information from the form $pic = ($_FILES['photo']['name']); //Writes the photo to the server if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ".

How to take image in PHP?

php file and show you layout of your webcam with "Take Snapshot" button, when you will click on that button js library will capture image on base64 string. after that when click on submit button then picture will store in directory. Read Also: PHP - How to create zip file and download using ZipArchive ?


1 Answers

Now i've solved it by myself =)

I forgot the backslashes in the body.

Here is the right body:

NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"\r\n"] 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]; 
like image 80
Michael Horner Avatar answered Oct 16 '22 04:10

Michael Horner