使用iphone中的多部分表单数据发布图像和其他数据

时间:2010-03-31 11:36:42

标签: php iphone objective-c

我正在使用目标C中的multipart / form-data向服务器发送一些数据和图像。 请给我一些Php代码,我怎么能在服务器上保存图像我能够获得我传递图像的服务器上的其他变量。请看我的obj C代码和php并告诉我我错在哪里。

您的帮助将受到高度赞赏。

这里我发出了POST请求。

//////////////////////

    NSString            *stringBoundary, *contentType, *baseURLString, *urlString;
    NSData              *imageData;
    NSURL               *url;
    NSMutableURLRequest *urlRequest;
    NSMutableData       *postBody;

    // Create POST request from message, imageData, username and password
    baseURLString   = @"http://localhost:8888/Test.php";
    urlString       = [NSString stringWithFormat:@"%@", baseURLString];  
    url             = [NSURL URLWithString:urlString];
    urlRequest      = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
    [urlRequest setHTTPMethod:@"POST"]; 

    // Set the params
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LibraryIcon" ofType:@"png"];
    imageData = [[NSData alloc] initWithContentsOfFile:path];

    // Setup POST body
    stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    contentType    = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
    [urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

    // Setting up the POST request's multipart/form-data body
    postBody = [NSMutableData data];

    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]];  // So Light Table show up as source in Twitter post

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:book.title] dataUsingEncoding:NSUTF8StringEncoding]];  // title

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"isbn\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:book.isbn] dataUsingEncoding:NSUTF8StringEncoding]];  // isbn

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"price\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:txtPrice.text] dataUsingEncoding:NSUTF8StringEncoding]];  // Price

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"condition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:txtCondition.text] dataUsingEncoding:NSUTF8StringEncoding]];  // Price


    NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
    //[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"\r\n\n\n"]dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [postBody appendData:imageData];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];


//  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"postBody=%@", [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding]);
    [urlRequest setHTTPBody:postBody];
    NSLog(@"Image data=%@",[[NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding]);

    // Spawn a new thread so the UI isn't blocked while we're uploading the image
    [NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest]; 

我的方法uploadDataWithURLRequest我将请求发布到服务器...

这是我的PHP代码

?php
    $title = $_POST['title'];
    $isbn  = $_POST['isbn'];
$price = $_POST['price'];
$condition = $_POST['condition'];
$image=$_FILES['image']['name'];



if($image)
{
    $filename = 'newimage.jpeg';
    file_put_contents($filename, $image);
    echo "image is there";
}
else
{
 echo "image is nil";
}


?> 

我无法在服务器上获取图像,请在我错的地方帮助我。

1 个答案:

答案 0 :(得分:1)

您正在错误地访问上传的文件。 $_FILE['image']['name']是客户端上传的文件的名称。您正在做的是将该名称(一个简单的字符串,如'test.jpg')写入$ filename,因此您的'newimage.jpeg'文件包含字符串,而不是JPEG数据。

您想要的是以下内容:

move_uploaded_file($filename, $_FILES['image']['tmp_name']);

$ _FILES数据的'tmp_name'部分是文件临时存储在服务器上的绝对路径(例如'/ var / tmp / sometemporaryuglyfilename'),然后您可以将其移动到最终的休息位置move_uploaded_file()以