将多个图像从Xcode上传到php服务器

时间:2015-01-12 04:29:11

标签: php ios xcode image

我想从我的Xcode上传多个图像到php服务器,当我发送1张图片时它运行良好并且发送超过1张图片时,php正在存储唯一的最后一张图像文件。请帮我解决这个问题。以下是我的IOS代码

   NSMutableURLRequest*request=[[NSMutableURLRequest alloc]init];

   [request setURL:url];

   [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

   [request setHTTPMethod:@"POST"];

   [request setHTTPShouldHandleCookies:NO];

   [request setTimeoutInterval:30];

   NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
   NSString*contentType=[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];

   [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

   NSMutableData*d=[NSMutableData data];

   NSMutableDictionary*parameters=[[NSMutableDictionary alloc]init];

   NSString*iD=[[NSUserDefaults standardUserDefaults]objectForKey:@"id"];

   NSString*fiD=[[NSUserDefaults standardUserDefaults]objectForKey:@"familyid"];

   NSString*eMailD=[[NSUserDefaults standardUserDefaults]objectForKey:@"email"];



   [parameters setObject:iD forKey:@"user_id"];

   [parameters setObject:self.modal.topicId forKey:@"topic_id"];

   [parameters setObject:self.posttextView.text forKey:@"message"];

   [parameters setObject:ddd forKey:@"posted_timestamp"];

   [parameters setObject:fiD forKey:@"family_id"];

   [parameters setObject:eMailD forKey:@"email_id"];

   [parameters setObject:fmem forKey:@"member_id"];


   NSString* FileParamConstant = @"filetoupload";


   NSArray*filetoupload=[[NSArray alloc]initWithObjects:FileParamConstant, nil];

   filetoupload=nil;

   for (NSString *param in parameters) {
       [d appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
       [d appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
       [d appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
   }

   NSLog(@"the chosen images count is %lu",(unsigned long)self.chosenImagesDataobjects.count);


   if (self.chosenImagesDataobjects.count>0) {

       for (int s=0;s<self.chosenImagesDataobjects.count ; s++) {


           [d appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
           [d appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image%d.jpg\"\r\n", FileParamConstant,s] dataUsingEncoding:NSUTF8StringEncoding]];
           [d appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
           [d appendData:[self.chosenImagesDataobjects objectAtIndex:s]];
           [d appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


           [d appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];


       }

   }


   [request setHTTPBody:d];


   NSLog(@"the body of the total payload is %@",d);

   NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[d length]];

   [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

   NSOperationQueue*eighthQueue=[[NSOperationQueue alloc]init];

    [NSURLConnection sendAsynchronousRequest:request queue:eighthQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

      if ([data length]>0) {

          NSLog(@"data greaterthan zero");

      }      

0 个答案:

没有答案