我正在尝试编写一个PHP脚本,该脚本应该接收iPad决定上传的任何文件。然后应将它们保存在正确的文件夹中。但是,每次iPad尝试上传时,都会显示“上传文件时出错”
这是服务器上的代码
<?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.";
}
?>
<?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.";
}
?>
以下是iPad上的代码:
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:&response error:&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];