title: Uploading image with parameters using multipart/form data in swift
body: <p>I am trying to upload an image at <strong>specific</strong> path of my server. That path will be determined by parameters sent from <code>multipart/form-data</code> request. </p>
  
  <p><strong>Problem</strong>: The image is not uploaded to the specified path, instead, uploaded to root of my server. </p>
  
  <p>I am calling an API that has a php function to save that image to the specified path. I think that PHP function is not getting the path from parameters correctly.</p>
  
  <p>API that i am calling looks something like</p>
  
  <p><a href="https://storage.myWebsiteName.com/upload_image.php/" rel="nofollow noreferrer">https://storage.myWebsiteName.com/upload_image.php/</a></p>
  
  <p>Contents of upload_image.php</p>
  
  <pre><code>   &lt;?php
  header('Access-Control-Allow-Origin: *'); 
  
  //Unable to get path parameter and store in @target_dir, instead this function stores image in root
  $target_dir = $_GET["path"];
  
  
  $name = basename($_FILES["fileToUpload"]["name"]);
  $target_file = $target_dir . $name;
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  // Check if image file is a actual image or fake image
  
  $actual_name = strtolower(
      pathinfo(
              $_FILES["fileToUpload"]["name"],PATHINFO_FILENAME));
  
  
      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
          echo $name;
      } else {
          echo "Sorry, there was an error uploading your file.";
      }
  
  ?&gt;
  </code></pre>
  
  <p>I am getting image from <code>UIImagePickerController</code> and passing the image to  this function which makes <code>multipart/form-data</code> Request</p>
  
  <pre><code>func postImageToDB(image : UIImage) {
  
      let imagePostUrlStr =  "https://myWebsiteName.com/upload_image.php/"
  
      guard let imageData = UIImagePNGRepresentation(image) else {
        return
      }
      //want to save my image to this directory which is inside root
      let params = ["path" : "Brainplow/001243192018125835"]
  
      Alamofire.upload(multipartFormData: { (multiPartFormData: MultipartFormData) in
        //append path parameter
        for (key, value) in params {
          multiPartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
        multiPartFormData.append(imageData, withName: "fileToUpload", fileName: "testfilename.png", mimeType: "image/png")
  
      }, to: imagePostUrlStr) { (result: SessionManager.MultipartFormDataEncodingResult) in
        switch result {
        case .success(request: let uploadRequest, _, _ ):
  
  
          uploadRequest.uploadProgress(closure: { (progress) in
  
            print("Upload Progress: \(progress.fractionCompleted)")
  
          })
  
          uploadRequest.responseString { response in
            print("printing response string")
            print(response.value as Any)
            print(response)
            print(response.result)
  
          }
  
        case .failure(let error):
          print(error.localizedDescription)
        }
      }
  
  
    }
  </code></pre>
  
  <p><strong>Note</strong>: Inside php function, if i set the directory to a <strong>constant</strong> string, it does save in that path. like if i do something below</p>
  
  <pre><code>$target_dir = "Brainplow"
  </code></pre>
  
  <p>That works fine, but i need to determine this directory from params sent from <code>multipart/form-data</code> request</p>
  
  <p>My image directory is as follows</p>
  
  <p><strong>Root Directory</strong>
  <a href="https://i.stack.imgur.com/HXiL7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HXiL7.png" alt="Root Directory"></a></p>
  
  <p><strong>Child Directory</strong></p>
  
  <p><a href="https://i.stack.imgur.com/3cCYq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3cCYq.png" alt="Child Directory"></a></p>
  
  <p><strong>Grand Child Directory</strong>
  <a href="https://i.stack.imgur.com/D4ueP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D4ueP.png" alt="Grand Child Directory"></a></p>
  
  <p>So, i want to be able to pass a path (example : "Brainplow/01113132018112642/") as a parameter to request</p>
  
  <p>PHP function should get the path from parameter and put the image there </p>
  
  <p>According to my research and struggle, issue is with PHP function. So maybe instead of using </p>
  
  <pre><code>$target_dir = $_GET["path"];
  </code></pre>
  
  <p>I may have to use </p>
  
  <pre><code>$target_dir = $_POST["path"];
  </code></pre>
  
  <p>But i don't know PHP much. But maybe there is another issue. But my swift code is working fine. Just my image is not being place in the path i am providing. </p>
  
  <p>Any help would be much appreciated</p>