如何在PHP中获取或POST多部分数据请求

我正在从iOS发送多部分请求上传图像或文档在服务器上。 我的要求是这样的

UIImage *im = [UIImage imageNamed:@"57X57.png"]; NSData *imageData = UIImageJPEGRepresentation(im, 90); NSString *urlString = @"my URL/upload.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"]; NSString *imgName = @"raj.png"; NSLog(@"image name : %@",imgName); NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@\"\r\n", imgName]] 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]; NSLog(@"setRequest : %@", request); NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"returnstring is : %@",returnString); 

并在PHP结束

 <?php print_r($_FILES); print_r($_POST); ?> 

当我写print_r($ _ FILES)时,它会在iOS端输出

 returnstring is : Array (    [files] => Array        (            [name] => raj.png            [type] => application/octet-stream            [tmp_name] => /tmp/phpI93DO3            [error] => 0            [size] => 366784        ) ) {"success":1} 

但在PHP服务器端数组是空的即Array()。

我是PHP中的Web服务开发的新手。 请告诉我我错在哪里。 我对iOS端代码(即向服务器发送多方请求)非常有信心,因为我使用相同的代码将多部分数据上传到基于Java的Web服务(Java中的RESTFull Web服务)。但是我对PHP方面没有太多的了解。 请指导我如何在PHP端获取多部分请求。

您好,请检查这[链接] 麻烦传递variables到PHP脚本上图片我已经发布了这个问题的答案已经PHP代码也有评论

供你参考

PHP代码

 $uploaddir = "../images/profile_images/"; $file = basename($_FILES['image']['name']); $uploadfile = $uploaddir . $file; if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) { $updt_img = "UPDATE tiny_users SET profile_photo = '".$file."' WHERE user_id = '".$final_res[1]."'"; mysql_query($updt_img); } 

上传多个图像到服务器使用下面的代码

 -(void)uploadMultiplePics { NSString *string ; NSData *imageData; NSString*urlString=[NSString stringWithFormat:@"http://******"]; // urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@""]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSMutableData *body; body = [NSMutableData data]; for(int j=0;j < scrollViewImageArray.count;j++) // scrollViewImageArray is images count { double my_time = [[NSDate date] timeIntervalSince1970]; int k=j+1; NSString *imageName = [NSString stringWithFormat:@"%d%d",j,(int)(my_time)]; NSString *imagetag=[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image%d\"; filename=\"",k]; string = [NSString stringWithFormat:@"%@%@%@", imagetag, imageName, @".jpg\"\r\n\""]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; UIImage *image=[[UIImage alloc]init]; image=[scrollViewImageArray objectAtIndex:j]; // scrollViewImageArray images array imageData = UIImageJPEGRepresentation(image, 90); [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*s= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

}

希望这会帮助你。