現在、Mac からサーバーに xml ファイルをアップロードしています。それは正常に動作します。以下はコードです:
osxコード
NSString *urlString1 = @"http://username:pwd@ip/files/upload.php";
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init];
[request1 setURL:[NSURL URLWithString:urlString1]];
[request1 setHTTPMethod:@"POST"];
NSString *boundary1 = @"---------------------------14737809831466499882746641449";
NSString *contentType1 = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary1];
[request1 addValue:contentType1 forHTTPHeaderField:@"Content-Type"];
NSMutableData *body1 = [NSMutableData data];
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary1] dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"data.xml\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[NSData dataWithData:xmlData]];
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary1] dataUsingEncoding:NSUTF8StringEncoding]];
[request1 setHTTPBody:body1];
NSLog(@"xml data %@",xmlData);
NSData *returnData1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];
およびPHP スクリプト:
<?php
$target_path = "./";
$target_path = $target_path.'data.xml';
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$target_path)) {
echo "The file has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
今、複数のファイルを追加することになっているケースがあります。では、ファイルの命名はどのように機能するのでしょうか? 誰でもこれについて私を助けてください。