我想上傳多張圖片到我的服務器使用Alamofire Lib,它只上傳1張圖片的問題。快速上傳多圖片到PHP服務器
我使用的圖像拾取返回UIImage的數組被命名爲
imagesdata
這是我的代碼:
@IBAction func uploadimages(_ sender: Any) {
Alamofire.upload(
multipartFormData: { multipartFormData in
for img in self.imagesdata{
let imgdata = UIImageJPEGRepresentation(img, 1.0)
multipartFormData.append(imgdata!,withName: "image", fileName: "image.jpg", mimeType: "image/jpeg")
print("$$$$$$$$$$ : \(imgdata!)")
}
},
to: "http://localhost/maarathtest/MAPI/img_upload.php",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
}
和我的PHP:
<?php
$response = array();
if (empty($_FILES["image"])) {
$response['File'] = "NOFILE";;
}else {
$filename = uniqid() . ".jpg";
// If the server can move the temporary uploaded file to the server
if (move_uploaded_file($_FILES['image']['tmp_name'], "images/" . $filename)) {
$response['status'] = "Success";
$response['filepath'] = "https://serverName/MAPI/images/" . $filename;
} else{
$response['status'] = "Failure";
}
}
echo json_encode($response);
?>
而我的控制檯日誌:
$$$$$$$$$$ : 5849743 bytes
$$$$$$$$$$ : 3253337 bytes
[Request]: POST http://localhost/maarathtest/MAPI/img_upload.php
[Response]: <NSHTTPURLResponse: 0x600000620940> { URL: http://localhost/maarathtest/MAPI/img_upload.php } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 101;
"Content-Type" = "text/html";
Date = "Thu, 25 May 2017 10:08:08 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.18 (Unix) OpenSSL/1.0.2h PHP/5.5.35 mod_perl/2.0.8-dev Perl/v5.16.3";
"X-Powered-By" = "PHP/5.5.35";
} }
[Data]: 101 bytes
[Result]: SUCCESS: {
filepath = "https://serverName/MAPI/images/5926ad083b770.jpg";
status = Success;
}
UPDATE:
我已經改變了如下我的代碼,
Alamofire.upload(
multipartFormData: { multipartFormData in
var count = 1
for img in self.imagesdata{
let imgdata = UIImageJPEGRepresentation(img, 1.0)
multipartFormData.append(imgdata!,withName: "image\(count)", fileName: "image\(count).jpg", mimeType: "image/jpeg")
count += 1
}
},...
<?php
$response = array();
if (empty($_FILES["image1"])) {
$response['File1'] = "NOFILE";
}else {
$filename = uniqid() . ".jpg";
// If the server can move the temporary uploaded file to the server
if (move_uploaded_file($_FILES['image1']['tmp_name'], "images/" . $filename)) {
$response['status1'] = "Success";
$response['filepath1'] = "https://serverName/MAPI/images/" . $filename;
} else{
$response['status1'] = "Failure";
}
}
if (empty($_FILES["image2"])) {
$response['File2'] = "NOFILE";
}else {
$filename = uniqid() . ".jpg";
// If the server can move the temporary uploaded file to the server
if (move_uploaded_file($_FILES['image2']['tmp_name'], "images/" . $filename)) {
$response['status2'] = "Success";
$response['filepath2'] = "https://serverName/MAPI/images/" . $filename;
} else{
$response['status2'] = "Failure";
}
}
echo json_encode($response);
?>
現在,它的上傳圖片,但我不認爲這是做了正確的方法,因爲我不知道用戶想要上傳多少圖片!
任何想法,幫助,代碼優化將是非常讚賞
在先進的感謝
檢查此 - https://stackoverflow.com/a/40907477/5172413 –
其不工作我已檢查我的PHP腳本的文件計數和其始終1 '$ response [「count」] = count($ _FILES ['image'] ['name']);' –
您將需要以不同的變量接收每個圖像文件。像 $ _FILES [ '圖像1'], $ _FILES [ '圖像2'],與 $ _FILES [ '圖像3'] 或 您可以發送圖像陣列和接收相同 –