post を使用してファイルをサーバーにアップロードする PHP ファイルがあります。最初は、このコードを使用しました
NSData *imageData = UIImageJPEGRepresentation([files objectAtIndex:i], 1.0);
NSString *urlString = "myDomain";
//[NSString stringWithFormat:@"%@test.php", delegate.dataBean.hosterURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"%@\"\r\n", [names objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog([NSString stringWithFormat:@"Image Return String: %@",returnString]);
そしてこのPHP。
<?php
$uploaddir = './RTF/';
$file = basename($_FILES['uploadedfile']['name']);
$uploadfile = $uploaddir . $file;
echo ">>$uploadfile<<\n";
echo "file=".$file; //is empty, but shouldn't
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo $file;
}
else {
echo "errorsjs";
}
?>
初めての設定でも問題ありませんでした。しかし今、この PHP の要件に合わせてコードを変更する必要があります。これは簡単だと思っていましたが、まったく取得できないようです。
//Receive the data from android
$target_path = $_POST['pathname'];
$new_name = $_POST['newname'];
//$target_path = "./RTF/";
//$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$target_path = $target_path . basename($new_name);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>