在iPhone应用程序中将图像上传到服务器

在iPhone应用程序中将图像上传到服务器

问题描述:

我想将图像上传到iPhone应用程序中,我正在使用以下PHP代码

I want upload image to sever in iphone app I am using following code for PHP

但是使用此代码,当我运行localhost/upload.php时,页面上会显示错误;

but using this code i get error displayed on page when i run localhost/upload.php;

  <?php
  $uploaddir = 'photos/';
  $file = basename($_FILES['userfile']['name']);
  $uploadfile = $uploaddir . $file;

 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "OK";
 else {
 echo "ERROR";
 }

  ?>

下面是iphone xcode代码

Below is the iphone xcode code

    - (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{


      NSString *urlString = @"http://localhost/upload.php";

      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:@"\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:[[NSString stringWithString:@"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];

     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
     NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

    return ([returnString isEqualToString:@"OK"]);
      }

调用方法

     [self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:@"pig.jpg"];

尝试一下,您是否也通过print_r($_FILES);

try this and also did you check in php by print_r($_FILES);

                    NSString *urlString = @"http://localhost/upload.php";

                    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
                    [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:@"\r\n--%@\r\n",boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
                    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
                    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                    [body appendData: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 *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];