Problem with PHP script to receive files from an iPad
<p>I am trying to write a PHP script that should receive any files that an iPad decides to upload. It should then save them in the correct folder. However, every time the iPad tries to upload, it says "Error uploading file"</p>

<p>Here is the code on the server</p>

<p><code></p>

<pre><code>&lt;?php

$target_path = "/Library/WebServer/Documents/uploads/";

echo "Target path is: ";
echo $target_path;
echo "\n";

echo "File name is: ";
echo basename($_FILES['userfile']['name']);
echo "\n";

$target_path = $target_path . $_FILES['userfile']['name'];

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "File uploaded OK.";
} else {
    echo "Problem with file upload.";
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
    echo "The file " . basename($_FILES['userfile']['name']) . "has been uploaded";
}
else
{
    echo "Error uploading file. Please try again."; 
}

?&gt;
</code></pre>

<p></code></p>

<p>Here is the code on the iPad:</p>

<p><code></p>

<pre><code>    int length = [postBody length];
NSString* length_str  = [NSString stringWithFormat: @"%d", length];
NSMutableURLRequest *request;
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding];
NSError* err = nil;
NSURLResponse *response = nil;
NSHTTPURLResponse *httpResponse;
NSData *dataReply;
id stringReply;
urlStr2 = [urlStr mutableCopy];
//[urlStr2 appendString:@"data.txt"];
NSLog(urlStr2);

request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr2]];

[request setHTTPMethod: @"POST"];
[request setHTTPBody:postData];
[request setValue:@"text/plain" forHTTPHeaderField:@"Accept"];
[request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"Checklist" forHTTPHeaderField:@"User-Agent"];
[request setValue:length_str forHTTPHeaderField:@"Content-Length"];


dataReply = [NSURLConnection sendSynchronousRequest:request
                                  returningResponse:&amp;response error:&amp;err];

if (err)
{
    NSLog(@"There was an error connecting to server.");
}

stringReply = (NSString *)[[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];

// Some debug code here
httpResponse = (NSHTTPURLResponse *)response;
int statusCode = 0;
if (httpResponse != nil) {
    NSLog(@"HTTP Response Headers %@", [httpResponse allHeaderFields]);
    statusCode = [httpResponse statusCode];
    NSLog(@"HTTP status code: %d", statusCode);
} else {
    NSLog(@"HTTP response is nil.");
}
// End of debug code

[pool release];
</code></pre>

<p></code></p>
