我想从我的iPhone应用上传一个plist文件到我的服务器。我尝试了下面的代码(发现谷歌搜索),但我的文件仍然没有上传。哪里有问题?iPhone上的文件上传问题
iPhone应用程序代码(方法,可处理上传):服务器
- (IBAction)sendHTTPPost:(id)sender {
NSString *path = [self pathOfFile];
NSString *fileName = @"Contacts";
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSString *urlString = @"http://localhost/ajaxim/fileUpload.php";
//set up request
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//required xtra info
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//body of the post
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
//lets make the connection
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
returnString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(returnString);
}
PHP代码:
<?php
$target = "./upload/";
$target = $target . basename($_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "YES";
echo "http://localhost/ajaxim/upload/{$target}";
}
else {
echo "Not uploaded";
}
?>
,请给我一些建议,我应该在哪里修改代码或者我错了?
我已经改变了它
乔敦 - R的人YourDirectoryName
搭配chmod -R 755 YourDirectoryName ..bt它不工作.. :( – Shahriar 2011-06-03 08:28:49
你可以检查你的'$ _POST'和'$ _FILES'数组吗?这是否提供线索? – sergio 2011-06-03 08:39:22