iphone mulipart form verilerini kullanarak görüntü ve diğer veri göndermek

1 Cevap php

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.

Lütfen yardım çok takdir edilecektir.

Burada i POST isteğini yapmak.

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

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

Ben sunucuya istek göndermek uploadingDataWithURLRequest yöntem ...

İşte benim 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";
}


?> 

Ben yanlış nerede sunucusunda görüntü nazikçe bana yardım alamıyor.

1 Cevap

Sen yanlış yüklenen dosya erişiyor. Istemci tarafından yüklenen gibi $_FILE['image']['name'] dosyanın adıdır. Ne yapıyoruz yüzden 'newimage.jpeg' dosya değil, bir dize JPEG veri içeriyor, $ dosya dışarı ('test.jpg' gibi basit bir dize) bu ismi yazıyor.

Ne istediğiniz şudur:

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

$ _FILES verilerin 'tmp_name' kısmı dosya sunucusu sonra move_uploaded_file ile nihai dinlenme yeri taşıyabilirsiniz (örn. '/ var / tmp / sometemporaryuglyfilename'), geçici olarak depolanan yere mutlak yolu () .