title: Sending audio from a Swift App to PHP Server, and somewhere the audio is lost
body: <p>I am making an App in Swift that records some audio and then sends that recording to my PHP server.</p>
  
  <p>The App records the audio clip fine (it can be played back with no problem). When I <code>println</code> the recorded audio clip it shows loads and loads of byte data (same when I put the audio into an <code>NSData</code> wrapper). This all suggests to me that the audio is fine inside the app.</p>
  
  <p>The PHP file catching the recording on my server also works fine and without errors.</p>
  
  <p>But somewhere along the line the recorded audio clip gets lost.</p>
  
  <p>Swift code that uploads the recording:</p>
  
  <pre><code>// The variable "recordedFileURL" is defined earlier in the code like this:
  
  currentFilename = "xxxx.m4a"
  let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
  let docsDir: AnyObject=dirPaths[0]
  recordedFilePath = docsDir.stringByAppendingPathComponent(self.currentFilename)
  recordedFileURL = NSURL(fileURLWithPath: self.recordedFilePath)
  
  // "currentFilename", "recordedFilePath" and "recordedFileURL" are all global variables
  
  // This recording stored at "recordedFileURL" can be played back fine.
  
  let sendToPath = "http://......../catch.php"
  let sendToURL = NSURL(string: sendToPath)
  let recording: NSData? = NSData(contentsOfURL: recordedFileURL)
  let boundary = "--------14737809831466499882746641449----"
  let contentType = "multipart/form-data;boundary=\(boundary)"
  
  var request = NSMutableURLRequest()
  request.URL = sendToURL
  request.HTTPMethod = "POST"
  request.addValue(contentType, forHTTPHeaderField: "Content-Type")
  request.addValue(recId, forHTTPHeaderField: "REC-ID") // recId is defined elsewhere
  
  var body = NSMutableData()
  var header = "Content-Disposition: form-data; name=\"\(currentFilename)\"; filename=\"\(recordedFilePath)\"\r\n"
  
  body.appendData(("\r\n-\(boundary)\r\n" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
  body.appendData((header as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
  body.appendData(("Content-Type: application/octet-stream\r\n\r\n" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
  
  body.appendData(recording!) // adding the recording here
  
  body.appendData(("\r\n-\(boundary)\r\n" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
  
  request.HTTPBody = body
  
  var session = NSURLSession.sharedSession()
  var task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -&gt; Void in
  
      println("upload complete")
      let dataStr = NSString(data: data, encoding: NSUTF8StringEncoding)
      println(dataStr)
  
  })
  
  task.resume()
  </code></pre>
  
  <p>PHP code in the file <code>catch.php</code> that is supposed to receive the recording:</p>
  
  <pre><code>$contents = file_get_contents('php://input');
  $files = $_FILES;
  
  echo "Caught the following:/r/n";
  echo "Contents:" . var_export($contents) . "/r/n";
  echo "Files:" . var_export($files) . "/r/n";
  </code></pre>
  
  <p>And whenever I run all of this I get the following output from <code>catch.php</code>:</p>
  
  <pre><code>Caught the following:
  Contents:''
  Files:array (
  )
  </code></pre>
  
  <p>So <code>catch.php</code> isn't receiving anything at all.</p>
  
  <p>Am I sending the recording wrong, or am catching the recording wrong? Or both?</p>
  
  <p>Thanks in advance.</p>