为什么NSURLSession uploadTaskWithRequest:fromData:无法上传到PHP服务器?

PHP代码工作正常。 我从同一台服务器上的HTML表单上传文件。 上传的文件从40K到2.0M不等。 file upload在运行PHP 5.3的服务器上激活

我发现这样的许多职位(没有答案): https : //stackoverflow.com/questions/19710388/using-nsurlsession-to-upload-an-image 。

这一个使用uploadTaskWithRequest:fromFile:而不是fromData: NSURLSession确保上传工作

这是NSURLSessionDataTask而不是uploadTaskWithRequest: 没有数据接收与NSURLSession

和一些似乎说uploadTaskWithRequest:fromData的post:根本不工作: NSURLSession:上传资产与背景传输 asynchronous上传与NSURLSession将无法正常工作,但同步NSURLConnection并将图像从iOS上传到PHP

我做了这样的应用程序,它返回的HTTP代码,我得到的代码200.服务器error_logfile upload后没有任何东西。 一切似乎工作正常,但无论发生什么,该文件不写入服务器。 任何想法还有什么我可以尝试找出什么是错的?

这里是php代码:

<?php $file=''; $uploaddir=''; //////////////// echo 'file count=', count($_FILES),"\n"; var_dump($_FILES); echo "\n"; //////////////// if(isset($_FILES['userfile']['name'])){ $uploaddir = './photos/'; //Uploading to same directory as PHP file $file = basename($_FILES['userfile']['name']); echo "file is set"; $uploadFile = $file; $randomNumber = rand(0, 99999); $newName = $uploaddir . $uploadFile; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { echo "Temp file uploaded. \r\n"; } else { echo "Temp file not uploaded. \r\n"; } if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) { $postsize = ini_get('post_max_size'); //Not necessary, I was using these $canupload = ini_get('file_uploads'); //server variables to see what was $tempdir = ini_get('upload_tmp_dir'); //going wrong. $maxsize = ini_get('upload_max_filesize'); echo "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ; } } ?> 

这里是iOS代码:

 - (void)uploadImage:(UIImage*)image { // 0 Define URL NSData *imageData = UIImageJPEGRepresentation(image, 0.6); NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/app/photos/uploadPhoto.php"]]; NSString *boundary = @"0xKhTmLbOuNdArY"; NSString *filename = [NSString stringWithFormat:@"someName.jpg"]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"POST"]; NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; // 4 Create object to put content into... NSMutableData *body = [NSMutableData data]; [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\" filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]]; //[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; //Not used with this:https://stackoverflow.com/questions/20893171/asynchronous-upload-with-nsurlsession-will-not-work-but-synchronous-nsurlconnect/20896632?noredirect=1#comment39074802_20896632 //[request setHTTPBody:body]; __block NSString *stringForText = @"Hola"; /////////////////////////////////////////////////// // 3 self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // ... NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; int errorCode = httpResponse.statusCode; NSString *errorStatus = [NSString stringWithFormat:@"%d",errorCode]; NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *totalResponse = [errorStatus stringByAppendingString:responseString]; stringForText = totalResponse; [self updateView:stringForText]; //UIAlertView *alertme = [[UIAlertView alloc] initWithTitle:@"error" message:totalResponse delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; //[alertme show]; // 4 self.uploadView.hidden = NO; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; }]; // 5 [_uploadTask resume]; } -(void)updateView:(NSString*)texto{ dispatch_sync(dispatch_get_main_queue(), ^{ self.myTextView.text = texto; }); } 

我有该文件夹的777权限。 正如我所说,我已经能够上传,从而通过一个HTML表单,从相同的PHP脚本写入该文件夹。

对于php转储,http响应错误码和count = 0以及array(0){}的总响应数是200。

以下是应用程序中服务器响应的图像: 在这里输入图像说明

我在回应临时file upload后添加了这些行:

 $body = $HTTP_RAW_POST_DATA; echo "\n REQUEST" . $body; 

但我只有这个:

 file count=1 array(1) { ["user file"]=> array(5) { ["name"]=> string(12) "DrinkCO2.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/tmp/phpxg7oLZ" ["error"]=> int(0) ["size"]=> int(190550) } } file is setTemp file uploaded. REQUEST 190550 image/png 

它没有错过一个冒号,它错过了一个分号。

 "name=\"userfile\" : filename=\"%@\"\r\n"" 

应该

 "name=\"userfile\"; filename=\"%@\"\r\n"" 

至less这是你的代码为我工作的唯一方式(顺便说一下,谢谢!

我在这里有一个分号问题 – >

 "name=\"userfile\" ; filename=\"%@\"\r\n"" 

所以它基本上是一个畸形的身体。