[英]Upload Image and Information (both) Using AFNetworking 2.0 in Single 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 for Images + Information上傳。
<?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);
?>
問題似乎在PHP方面
$json = file_get_contents('php://input'); // Catching input
$value= json_decode($json, true); // Decode JSON into Dictionary.
不工作。 用此行替換上面的塊
$value= $_REQUEST;
應該解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.