Php 在swift中使用AFN网络上传多幅图像
我想使用swift中的Php 在swift中使用AFN网络上传多幅图像,php,ios,swift,afnetworking,Php,Ios,Swift,Afnetworking,我想使用swift中的AFNetworking将多个图像上载到我的网站,但仅上载数组中的最后一个图像 swift脚本: let url = "http://pathtomysite" let afHTTP : AFHTTPRequestSerializer = AFHTTPRequestSerializer() let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POS
AFNetworking
将多个图像上载到我的网站,但仅上载数组中的最后一个图像
swift脚本:
let url = "http://pathtomysite"
let afHTTP : AFHTTPRequestSerializer = AFHTTPRequestSerializer()
let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in
var i = 0
for image in upImage {
let imageData : NSData = UIImageJPEGRepresentation(image as UIImage, 0.5)!
formData.appendPartWithFileData(imageData, name: "uploaded_file", fileName: "imagex\(i)x.png", mimeType: "image/png")
i++
}
}, error: nil)
let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, AnyObject, error) -> Void in
if (error != nil){
print("error")
}
}
uploadTask.resume()
<?php
$dt = date("Ymdhis");
$fileInfo = pathinfo($_FILES['uploaded_file']['name']);
$file_path = "uploads/";
$file_path = $file_path . basename($_FILES['uploaded_file']['name']);
if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
rename($file_path, 'uploads/' . $dt . '.' . $fileInfo['extension']);
}
?>
php脚本:
let url = "http://pathtomysite"
let afHTTP : AFHTTPRequestSerializer = AFHTTPRequestSerializer()
let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in
var i = 0
for image in upImage {
let imageData : NSData = UIImageJPEGRepresentation(image as UIImage, 0.5)!
formData.appendPartWithFileData(imageData, name: "uploaded_file", fileName: "imagex\(i)x.png", mimeType: "image/png")
i++
}
}, error: nil)
let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, AnyObject, error) -> Void in
if (error != nil){
print("error")
}
}
uploadTask.resume()
<?php
$dt = date("Ymdhis");
$fileInfo = pathinfo($_FILES['uploaded_file']['name']);
$file_path = "uploads/";
$file_path = $file_path . basename($_FILES['uploaded_file']['name']);
if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
rename($file_path, 'uploads/' . $dt . '.' . $fileInfo['extension']);
}
?>
试试这个:
let url = "YOUR_URL"
let afHTTP : AFHTTPRequestSerializer = AFHTTPRequestSerializer()
let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in
for (index, image) in upImage.enumerate() {
let imageData = UIImageJPEGRepresentation(image as UIImage, 0.5)!
formData.appendPartWithFileData(imageData, name: "uploaded_file[]", fileName: "imagex\(index)x.png", mimeType: "image/png")
}
}, error: nil)
let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, AnyObject, error) -> Void in
if (error != nil){
print("error")
}
}
uploadTask.resume()
并将脚本更改为以下内容:
foreach ($_FILES["uploaded_file"]["tmp_name"] as $index => $tmp_name) {
$filePath = "uploads/" . basename($_FILES["uploaded_file"]["name"][$index]);
if (move_uploaded_file($tmp_name, $filePath)) {
// rename like you want to
}
}
重要的一点是在上传代码中的上传文件[]中添加括号。如果不包括[]每个图像上载都会覆盖最后一个图像上载
另一个重要部分是脚本中的foreach循环,它处理多个上传的图像,而不是一个