Reputation: 318
I want to upload a plist file to my server from my iphone app. I have tried the following code (found googling), but my file is still not uploaded. Where is the problem?
iphone app code (method which handles uploading):
- (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 code for server:
<?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";
}
?>
please give me some suggestion where should i change the code or where am i wrong?
Upvotes: 1
Views: 970
Reputation: 666
I know this is a little late:
I didn't see where you gave us the type of server you are using.
If using unix or linux servers. Many people, myself included, forget to check is the permissions of the directory you are using. You want to make sure you have the proper permissions to add, move, and delete files.
This can done safely by navigating to the directory that contains your upload folder with your shell and run the following commands:
chown -R nobody YourDirectoryName
chmod -R 755 YourDirectoryName
Upvotes: 0
Reputation: 69027
It seems to me that you are posting the file in as userfile
, and trying to read it server side as uploaded
.
Make this uniform and it should work.
Upvotes: 2