Загрузить изображение с iOS на PHP

Я пытаюсь загрузить изображение из моего приложения iOS на свой веб-сервер через PHP. Вот следующий код:

-(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 } } 

И следующий простой простой PHP-код:

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

Приложение на iOS, кажется, загружает фотографию, и рабочий стол работает, но я не загружаюсь в файл, когда проверяю файлы сервера.

Когда я отправляю изображение со следующим кодом, он отлично работает (EDIT: но без панели прогресса):

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

И идея, где я ошибаюсь?

    Наконец, я использовал библиотеку AFNetworking чтобы справиться с этим. Поскольку я не нашел ясных методов для этого в Интернете и в stackoverflow, вот мой ответ, чтобы легко отправлять изображения пользователя с их устройства iOS на ваш сервер через PHP. Большинство кода поступает из этой записи stackoverflow .

     -(void)uploadImage { image = [self scaleImage:image toSize:CGSizeMake(800, 800)]; NSData *imageData = UIImageJPEGRepresentation(image, 0.7); // 1. Create `AFHTTPRequestSerializer` which will create your request. AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; NSDictionary *parameters = @{@"your_param": @"param_value"}; NSError *__autoreleasing* error; // 2. Create an `NSMutableURLRequest`. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://www.yoururl.com/script.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"image.jpg" mimeType:@"image/jpg"]; } error:(NSError *__autoreleasing *)error]; // 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure %@", error.description); }]; // 4. Set the progress block of the operation. [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { //NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite); [self.progressBarView setProgress:(double)totalBytesWritten / (double)totalBytesExpectedToWrite animated:YES]; }]; // 5. Begin! operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"]; self.progressView.hidden = NO; [operation start]; } 

    Я думаю, это поможет новым xcoders.

    Приветствия.