我正在尝试使用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 for Images +信息上传。
<?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 :(得分:0)
问题似乎在PHP方面 线条
$json = file_get_contents('php://input'); // Catching input
$value= json_decode($json, true); // Decode JSON into Dictionary.
不行。用这一行替换上面的块
$value= $_REQUEST;
应解决您的问题。