0

我有一个 PHP 文件,它使用 post 将文件上传到服务器。最初,我使用了这段代码

 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 回答