我试图在Alamofire库的帮助下将一组少量图像(图像阵列)和参数上传到服务器。问题是我想上传具有相同ID但名称不同的图像,我与此完全相反。我还提到了一些在堆栈溢出时回答的问题,但这些问题仅适用于单个图像上传。请帮朋友。提前谢谢。
上传功能
let parameters = [
"defect_id" : "10",
"file_name" : self.dateMashup(1)
]
Alamofire.upload(
multipartFormData: { multipartFormData in
var count = 1
for img in self.photoArray{
let imgdata = UIImagePNGRepresentation(img)
// the name should be as array other wise want work
let randsome = self.dateMashup(count)
multipartFormData.append(imgdata!,withName: "image[]", fileName: "image\(randsome).png", mimeType: "image/png")
count += 1
}
for (key, value) in parameters {
multipartFormData.append(value.data(using: .utf8)!, withName: key)
}
},
to: "http://localhost/Source2/ImageDir/uploadImages.php",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseString { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
PHP文件
<?php
$server = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "lmg_new";
$con = mysqli_connect($server, $dbuser, $dbpassword, $dbname);
$defect_id=$_POST["defect_id"];
$file_name = $_POST["file_name"];
$created = date('Y-m-d H:i:s');
// $defect_id = 1;
// $file_name = "sadas.png";
// $created = date('Y-m-d H:i:s');
$sql = "INSERT INTO `files`(`defect_id`, `file_name`, `created`) VALUES (defect_id, file_name, created)";
$result= mysqli_query($con, $sql) or die("query fail:". mysqli_error($con));
// $response = array();
$response["success"]= true;
if($result){
while($row = mysqli_fetch_assoc($result))
{
$flag[]=$row;
}
$response['data'] = $flag;
}
if (empty($_FILES["image"])){
$response['File1'] = "NOFILE";
}else{
foreach ($_FILES["image"]["tmp_name"] as $index => $tmp_name) {
$filePath = "images/" . basename($_FILES["image"]["name"][$index]);
if (move_uploaded_file($tmp_name, $filePath)) {
$response['filePath'][$index] = $filePath;
}
$response['status'] = "Success";
}
}
echo json_encode($response);
?>