开发者

post image and other data using mulipart form data in iphone

I am sending some data and and an image to the server using multipart/form-data in objective C. kindly give me some Php code that how can i save the image on the server i am able to get the other variables on the server that i am passing with the image. kindly see my obj C code and php and tell me where i am wrong.

your help will be highly appreciated.

here i make the POST request.

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

    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:NSU开发者_如何学JAVATF8StringEncoding]];  // 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]; 

I the method uploadingDataWithURLRequest i post the request to the server...

Here is my php Code

?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";
}


?> 

I am unable to get the image on server kindly help me where i am wrong.


You're accessing the uploaded file incorrectly. $_FILE['image']['name'] is the name of the file as uploaded by the client. What you're doing is writing that name (a simple string, like 'test.jpg') out to $filename, so your 'newimage.jpeg' file contains a string, not JPEG data.

What you want is the following:

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

the 'tmp_name' portion of the $_FILES data is the absolute path to where the file was stored temporarily on the server (e.g. '/var/tmp/sometemporaryuglyfilename'), which you can then move to its final resting place with move_uploaded_file().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜