2014-06-21 35 views
1

我试图使用AFNetworking 2.0上传多张图片(产品图片)和信息(品牌名称,价格等)。上传图片和信息(都)在单个PHP中使用AFNetworking 2.0

我还是设法上传使用两个单独的PHP文件
1. upload_product_info.php
2. upload_product_images.php,在成功呼叫upload_product_images.php

第一个电话upload_product_info.php图像和信息。一切工作正常,但我希望它使用单个PHP文件。

我试过单一的PHP,但它部分工作,没有给出任何错误只有上传图片,数据库中的产品信息字段(产品品牌,名称等)总是空白。我不明白我在做什么错。我是新手在PHP。

这里是我的iOS代码来上传图片和信息。

- (IBAction)uploadProduct:(id)sender { 

// for Testing purpose just taken 2 fields. 
    NSString *productbrand = @"xyz"; 
    NSString *productname = @"pqr"; 

    NSDictionary *infoDictionary = @{@"pbrand": productbrand, @"pname": productname}; 

    __block NSUInteger success = 0; 
    __block NSString *message; 
    static int count = 1; 

// returns array of product images url from temp Directory. 
    productImages = [self returnImagesFromTemporaryDirectory]; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

    manager.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted]; 
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 

    [manager POST:@"http://localhost/~abc/Website/uploadProduct.php" parameters:infoDictionary constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
    { 
    for (NSURL *filePath in productImages) 
    { 

     CFStringRef pathExtension = (__bridge_retained CFStringRef)[filePath pathExtension]; 
     CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL); 
     CFRelease(pathExtension); 

     NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType); 
     NSLog(@" Mime Type : %@", mimeType); 

     NSString *imageName = [NSString stringWithFormat:@"IMG00%i.png",count]; 
     count++; 

     [formData appendPartWithFileURL:filePath name:@"uploaded_file[]" fileName:imageName mimeType:mimeType error:nil]; 
    } 
    } 
    success:^(AFHTTPRequestOperation *operation, id responseObject) 
    { 
    NSDictionary *responseDic = (NSDictionary *)responseObject; 

    success = [responseDic[@"success"] integerValue]; 
    NSLog(@"Success: %ld",(long)success); 
    message = responseDic[@"message"]; 

    if (success == 1) 
    { 
     UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@" Success " message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

     [successAlert show]; 
    } 

    } 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    { 
    NSLog(@"Error: %@ ***** %@", operation.responseString, error); 

    UIAlertView *failedAlert = [[UIAlertView alloc] initWithTitle:@" Failed " message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

    [failedAlert show]; 
    }]; 

这里是我的PHP的图像+信息上传。

<?php 

    header('Content-type: application/json'); 

    $json = file_get_contents('php://input'); // Catching input 
    $value= json_decode($json, true); // Decode JSON into Dictionary. 

    $response = array(); 

// retrieve values from Dictionary using key. 
    $productBrand = $value['pbrand']; 
    $productname = $value['pname']; 


    // Database Connection. 
    $mysqlserver="localhost"; 
    $mysqlusername="abc123"; 
    $mysqlpassword="pqr123"; 
    $link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); 

    $dbname = 'myDatabase'; // change this to the name of your database 
    mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error()); 

    // Insert Data into Table. 
    $insertQuery = "INSERT INTO user_test (productBrand, productName) VALUES ('$productBrand', '$productname')"; 

    $result = mysql_query($insertQuery); 

    if($result) 
    { 
     $count=0; 

      foreach ($_FILES['uploaded_file']['name'] as $filename) 
      { 
       $file_path="uploads/";    

       $tmp=$_FILES['uploaded_file']['tmp_name'][$count]; 

       move_uploaded_file($tmp,$file_path.$filename);   

      $count=$count + 1; 
      } 

      $response["success"] = 1; 
      $response["message"] = "Images uploaded Successfully."; 
    } 
    else 
    { 
      $response["success"] = 0; 
      $response["message"] = "Failed to upload Images"; 
    } 

    echo json_encode($response); 

    ?> 

回答

0

问题似乎是在PHP端 线

$json = file_get_contents('php://input'); // Catching input 
$value= json_decode($json, true); // Decode JSON into Dictionary. 

不起作用。用这条线代替上述块

$value= $_REQUEST; 

应该可以解决您的问题。

+0

谢谢wooknight。它解决了我的问题,我非常感谢你的帮助。 – Linus