0

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!";
    }
?>
4

1 に答える 1

0
NSString *urlstring=[appDelegate.SmartAutoString 
 stringByAppendingPathComponent:[NSString stringWithFormat:@"smartautoXml.php"]];

NSURL *nsurl =[NSURL URLWithString:urlstring];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:nsurl];
[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];

if (dataImage) {
    NSLog(@"imageCounter %i",imageCounter);

    [body appendData:[[NSString stringWithFormat:
              @"Content-Disposition: form-data; name=\"imgAryId\"\r\n\r\n%i",
                                                                imageCounter]
                              dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]
                               dataUsingEncoding:NSUTF8StringEncoding]];


    img22=[UIImage imageWithData:dataImage];
    NSLog(@"dataImage %@  %@",dataImage,img22);

    NSString *temp=[NSString stringWithFormat:
                       @"Content-Disposition: form-data; name=\"img\";
                        "filename=\"image%i.jpg\"\r\n",imageCounter];
    [body appendData:[[NSString stringWithString:temp] 
                              dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"temp %@",temp);
    // [body appendData:[@"Content-Disposition: form-data; name=\"image\";
    //                    "filename=\"image0.jpg\"\r\n" 
    //dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" 
   dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:dataImage]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] 
   dataUsingEncoding:NSUTF8StringEncoding]];

 }
于 2012-12-27T12:13:34.630 に答える