iPhone upload picture to PHP file strange behavior
<p>Hey guys, I've been trying to upload pictures from my iPhone app to my server, but without concrete results. I once made it, but it seems like now it's not working anymore.</p>

<p>The code of my upload picture in the iPhone is the following:</p>

<pre><code>- (void) uploadPicture: (UIImageView *) newImage withParams: (NSString *) params{
//newImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://farm3.static.flickr.com/2662/4149796861_80391f5364.jpg"]]];

NSData *imageData = UIImageJPEGRepresentation(newImage.image, 90);
NSString *urlString = [@"http://my.server/mobile_interface.php?" stringByAppendingString: params];

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type
 */
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\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]];
// setting the body of the post to the request
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);
[self setImageTaken: nil];
}
</code></pre>

<p>As you can see the code is pretty simple, I just set up a connection and I use a POST to send the thing. I use POST for posting the image, while the parameters are appended, so I use GET to get them.</p>

<p>On the PHP side the code is the following:</p>

<pre><code>if(isset($_GET["action"]) &amp;&amp; $_GET["action"] == "upload_picture_new_user" &amp;&amp; isset($_GET["user_id"])){
$uploaddir = '/my/path/';
echo $uploaddir;

$image = $_FILES['userfile']['name'];
$file = basename($_FILES['userfile']['name']);
//find extension of the file
$ext = findexts($file);

echo $file;
echo $ext;

$ran = rand (); 
$file = $ran;

$filename = $file .'.'. $ext;

echo $filename;

$uploadfile = $uploaddir . $file .'.'. $ext;
/*$filePrev = $file.'_prev';
$uploadprev = $uploaddir . $filePrev .'.'. $ext; */

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "uploaded_images/".$filename;
    $query = "INSERT INTO `my_table` (`profilepic`) VALUES ('uploaded_images/".$filename."')";
            //do the query
    do_query($query);
}
}
</code></pre>

<p>As you can see from this code I just take the FILES variable and take out the extension, then I upload the file.</p>

<p>My problem is that nothing is uploaded and moreover the echo's that are throughout the code are not printed in the console of my iPhone simulator when I do "NSLog(returnString);". </p>

<p>Does anyone of you can get where the error is? I believe it's a stupid error, but you know, when keeping watching the same code for more than one hour then everything seems correct and you get frustrated even though the error is just under your eyes.</p>

<p>Thanks a lot.</p>
