Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在swift中使用AFN网络上传多幅图像_Php_Ios_Swift_Afnetworking - Fatal编程技术网

Php 在swift中使用AFN网络上传多幅图像

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

我想使用swift中的
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循环,它处理多个上传的图像,而不是一个