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

时间:2012-03-14 04:36:14

标签: iphone objective-c

我希望上传图片在iphone应用程序中切断我正在使用以下PHP代码

但是当我运行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代码

    - (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"];

2 个答案:

答案 0 :(得分:0)

如果你认为PHP代码中的错误,那么首先尝试这个print_r($_FILES);,看看你在$_FILES['userfile']

中得到了什么

答案 1 :(得分:0)

试试这个,你也通过print_r($_FILES);

检查了php
                    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];