Laden Sie ein Bild von iOS nach PHP hoch

Ich versuche, ein Bild von meiner iOS-App über PHP auf meinen Webserver hochzuladen. Hier ist der folgende Code:

-(void)uploadImage {
    NSData *imageData = UIImageJPEGRepresentation(image, 0.8);   

    //1
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

    //2
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];


    NSString *urlString = @"http://mywebserver.com/script.php";
    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:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iosfile.jpg\"\r\n" 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];

    //3
    self.uploadTask = [defaultSession uploadTaskWithRequest:request fromData:imageData];

    //4
    self.progressBarView.hidden = NO;
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    //5
    [uploadTask resume];
}

// update the progressbar
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.progressBarView setProgress:(double)totalBytesSent / (double)totalBytesExpectedToSend animated:YES];
    });
}

// when finished upload
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        self.progressBarView.hidden = YES;
        [self.progressBarView setProgress:0.0];
    });

    if (!error) {
        // no error
        NSLog(@"no error");
    } else {
        NSLog(@"error");
        // error
    }

}

Und der folgende funktionierende einfache PHP-Code:

<?php
$msg = " ".var_dump($_FILES)." ";
$new_image_name = $_FILES["userfile"]["name"];
move_uploaded_file($_FILES["userfile"]["tmp_name"], getcwd() . "/pictures/" . $new_image_name);
?>

Die Anwendung auf iOS scheint das Foto hochzuladen und die Fortschrittsanzeige funktioniert, aber ich habe die Datei nicht wirklich hochgeladen, wenn ich die Serverdateien überprüfe.

Wenn ich das Bild mit folgendem Code sende, funktioniert es einwandfrei (BEARBEITEN: aber ohne Fortschrittsbalken):

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Und wo irre ich mich?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage