tags:

views:

353

answers:

2

I try to send data to this php script:

 mb_internal_encoding("UTF-8");

    if(isset($_POST['format']) && isset($_POST['category']) && isset($_POST['title']) && isset($_POST['description']) && isset($_FILES['photo']) {
     save($_POST['category'], $_POST['title'], $_POST['description'], $_FILES['photo']); 
    } else {
     echo "There was an error, a field does not exist, please try again!<br />";
     echo "format = " . $_POST['format'] . "<br />";
     echo "category = " . $_POST['category'] . "<br />";
     echo "title = " . $_POST['title'] . "<br />";
     echo "description = " . $_POST['description'] . "<br />";
     echo "photo =" . $_FILES['photo'] . ;
    }
...
...

And I'm trying to send a syncronousRequest with this Objc code from iPhone:

//creating the url request:
 NSURL *cgiUrl = [NSURL URLWithString:@"http://www.hhh.com/uploading.php"];
 NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];

 //adding header information:
 [postRequest setHTTPMethod:@"POST"];

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


 //setting up the body:
 NSMutableData *postBody = [NSMutableData data];
 [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"category\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"iPhone"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"format\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:self.theCaption] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.theTitle] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[NSData dataWithData:UIImageJPEGRepresentation(resizedImage(self.myPhoto, CGRectMake(0, 0, 600, 800)), 0.5)]];
 [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

 [postRequest setHTTPBody:postBody];

 NSError *theError;
 NSData *returnData = [ NSURLConnection sendSynchronousRequest: postRequest returningResponse: nil error:&theError ];
 NSString *returnDataString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

 NSLog(@"[DEBUG]... response from request: %@", returnDataString);

But the answer is:

[DEBUG]... response from request: There was an error, all field not existe, please try again!
format =
category =
title =
description =
photo =latitude =
longitude =

What am I doing wrong? because what I see is that no field is received.

+1  A: 

You'd probably get a lot further if your PHP script logged what it actually did receive rather than how it interpreted it.

Azeem.Butt
Yes, I'm using another script and I send one value at a time, there I found that the failing part happens when I send the file... I'll continue on this. Thanks
Carlos
A: 

Well ... one problem was solved. The format between parameters must be like this:

@"\r\n--%@\r\n"

and I was sending:

@"\r\n--%@--\r\n" // two more scores

Still now I can't send the data. I've made a new script with only this content in the same server.

<?php

        echo "format = " . $_POST['format'];
      echo "[NEXT]... category = " . $_POST['category'];
      echo "[NEXT]... title = " . $_POST['title'];
      echo "[NEXT]... description = " . $_POST['description'];
      echo "[NEXT]... latitude = " . $_POST['latitude'];
      echo "[NEXT]... longitude =" . $_POST['longitude'];

?>

and copied the code to the original script (which was written by other guy) .

But now I see that one script receives the data and the original can't.

Could it be a fle encoding problem?

Carlos
New discover: the url that I was using was @"http://www.website.com/uploading.com"and the correct url is @"http://website.com/uploading.com"Next step is find why the picture isn't being uploaded.
Carlos
Ok, that everything with this, the problem is solved
Carlos