Search code examples
phpiosswiftafnetworking

Upload multiple images using AFNetworking in swift


I want to upload multiple images to my website using AFNetworking in swift, but only the last image in the array uploaded .

swift script:

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 script:

<?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']);
} 

?>

Solution

  • try this:

    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()
    

    and change your script to the following:

    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
        }
    }
    

    the important piece is adding brackets to the uploaded_file[] in your upload code. if you do not include the [] every image upload overrides the last one.

    the other important piece is the foreach loop in your script that handles multiple uploaded images instead of just one.