如何将文件目录中的图像文件上传到iphone中的php服务器 [英] how to upload image file from document directory to php server in iphone

查看:101
本文介绍了如何将文件目录中的图像文件上传到iphone中的php服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发类似iPhone的应用程序。根据我的要求,我需要将所有摄像头捕获的图像存储到文档目录中。文档文件夹中有大约10-20个图像文件,之后我必须使用单个php URL在服务器上推送该应用程序资源文档文件夹。我附上了用于显示包含一些图像文件的资源文件夹的屏幕截图。





这是我的PHP代码:

  $ file_path =../ Gallery /; 

$ file_path = $ file_path。 basename($ _FILES ['Documents'] ['name']);
if(move_uploaded_file($ _ FILES ['Documents'] ['tmp_name'],$ file_path)){
echoImage is upload;
} else {
echo图片不上传;
}

有人可以帮助我如何上传php服务器上的文件夹?



提前致谢。

解决方案

你需要使用/来电用于完成此操作的递归函数中的API。


Serverside Code




  //在服务器中创建一个名为images的文件夹,用于上传图像。 
//并创建一个PHP文件并使用下面的代码。


<?php
$ uploaddir ='images /';
$ ran = rand();

$ file = basename($ _ FILES ['userfile'] ['name']);
$ uploadfile = $ uploaddir。$ ran。$ file;

if(move_uploaded_file($ _ FILES ['userfile'] ['tmp_name'],$ uploadfile)){
echowww.host.com/.../images/{$上传文件};
}
?>




申请方代码




   - (IBAction)uploadClicked:(id)sender 
{

[self UploadTheImageNow:0] ;

}

以下代码用于递归函数,该函数将持续到所有图片上传都将完成。

   - (void)UploadTheImageNow:(NSInteger)count 
{

// UIImage * updatedImage = [UIImage ImageNamed:@您的图像名称/您可以从数组中获取];
UIImage * updatedImage = [UIImage ImageNamed:[yourArray ObjectAtIndex:ic]];
NSData * imageData = UIImageJPEGRepresentation(updatedImage,90);
NSString * urlString = @你的URL链接;
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[请求setHTTPMethod:@POST];


NSString * boundary = [NSString stringWithString:@--------------------------- 14737809831466499882746641449 ];
NSString * contentType = [NSString stringWithFormat:@multipart / form-data; boundary =%@,boundary];
[请求addValue:contentType forHTTPHeaderField:@Content-Type];
NSMutableData * body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@rn - %@ rn,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Disposition:form-data; name =userfile; filename =ipodfile.jpgrn] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Type:application / octet-streamrnrn] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@rn - %@ - rn,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[请求setHTTPBody:body];


[NSURLConnection sendAsynchronousRequest:请求队列:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *响应,NSData *数据,NSError *错误)
{
if([(NSHTTPURLResponse *)response statusCode] == 200)
{
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@使用jsonObject%@完成,jsonObject);
count ++;
if(count< 20)
{
[self UploadTheImageNow:count];
}
其他
{
//已完成上传所有图片
}

}

}];


}


I am currently working on gallery kind of iPhone application. According to my requirement, i need to store all the camera captured image into document directory. there is some 10-20 image files in document folder and after that i have to push that application resource document folder on server using single php url. I have attached screenshot for displaying resource folder with some image files.

here is my PHP Code :

  $file_path = "../Gallery/";

   $file_path = $file_path . basename( $_FILES['Documents']['name']);
   if(move_uploaded_file($_FILES['Documents']['tmp_name'], $file_path)) {
       echo "Image is upload";
   } else{
       echo "Image is not Upload";
   }

Can someone help me how can i upload document folder on php server ?

Thanks in advance.

解决方案

You need to use/ call the API in a recursive function to get this done.

Serverside Code

//Create a folder named images in your server where you want to upload the image.
 // And Create a PHP file and use below code .


 <?php
 $uploaddir = 'images/';
 $ran = rand () ;

 $file = basename($_FILES['userfile']['name']);
 $uploadfile = $uploaddir .$ran.$file;

 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
 echo "www.host.com/.../images/{$uploadfile}";
 }
 ?>

Application Side code

 - (IBAction)uploadClicked:(id)sender
 {

 [self  UploadTheImageNow:0];

 }

The below code is for the recursive function which will continue till all the image upload will get finish.

-(void)UploadTheImageNow:(NSInteger)count
 {

 // UIImage *updatedImage=[UIImage ImageNamed:@" Your Image name / you can get from array"];
 UIImage *updatedImage=[UIImage ImageNamed:[yourArray ObjectAtIndex:ic]];
 NSData *imageData = UIImageJPEGRepresentation(updatedImage, 90);
 NSString *urlString = @"your URL link";
 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"];


 NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
 [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
 NSMutableData *body = [NSMutableData data];
 [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
 [body appendData:[NSData dataWithData:imageData]];
 [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [request setHTTPBody:body];


 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
 completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
   if ([(NSHTTPURLResponse *)response statusCode]==200)
    {
      id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
      NSLog(@"Finished with jsonObject %@", jsonObject);
      count++;
      if(count<20)
         {
           [self  UploadTheImageNow:count];
         }
        else
          {
            // Finished Uploading all Images
          }

    }

 }];


 }

这篇关于如何将文件目录中的图像文件上传到iphone中的php服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆