Resolved Upload an audio file to a server using Swift


macrumors newbie
Original poster
Jan 9, 2014
Very new to iOs development and Swift here!

I've been asked by a company I intern for to make an iOs app where the user records a few sentences-worth of audio, is able to listen back to said audio, and either re-record it or submit it. If they choose to submit it, the audio file is emailed to the company.

To accomplish this I've been using XCode 6.2 and Swift, using an iOs Simulator for testing.

So far I've accomplished everything but the "submit" part. I want to be able to temporarily upload the audio file to the company website server, and then send that file to the email address as an attachment using PHPMailer (please correct me if there's a better way). The problem is I can't seem to figure out how to upload the file.

In summary:

Step 1:
After recording, the audio file is saved on my simulated phone with the path:


Step 2:
Use Swift to upload the audio file to the company's server (need help with this)

Step 3:
Use a PHPMailer script (something like below) to automatically email that file to a member of the company.

if (!empty($_POST)) {
    require '/PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;

    $mail->Host = 'MY MAIL HOST';
    $mail->SMTPAuth = true;
    $mail->Username = 'MY USERNAME';
    $mail->Password = 'MY PASSWORD';
    $mail->SMTPSecure = 'ssl';
    $mail->SMTPKeepAlive = true;
    $mail->Port = 465;

    $mail->From = 'FROM ADDRESS';
    $mail->FromName = 'FROM NAME';
    $body =     'Here is your file';

    $mail->Subject = 'Uploaded audio';
    $mail->Body    = $body;

So can anyone just point me in the right direction for uploading an audio file to a server using Swift? From research, it looks like I should be using a library such as Alamofire or SwiftHTTP, but I'm yet to find a tutorial that explains sending an audio file (they all seem to be for images). Let me know if you have any advice!

Last edited:


macrumors newbie
Original poster
Jan 9, 2014
For anyone curious, I've managed to figure it out.

First I followed the procedure in this tutorial for uploading images, making sure it worked.

I then basically modified the code to refer to audio rather than images.

For instance, I changed the file path in myImageUploadRequest() to:

var fileURL = NSURL(fileURLWithPath: audiofilePath)
As well as changed the destination file and mime type:
let filename = "file.mp3"
let mimetype = "audio/mpeg"
(note, I tested this with an mp3 instead of an m4a as in my original post)

My PHP file looks like this:
$target_dir = "./uploads";

mkdir($target_dir, 0777, true);

$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) {
echo json_encode([
"Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.",
"Status" => "OK"

} else {

echo json_encode([
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error"
After adjusting my php.ini to allow files larger than 2M, the upload works perfectly.

I suppose this is probably pretty intuitive, but hopefully this will help other newcommers, since there aren't any audio upload tutorials for Swift that I've found, only image upload.


macrumors newbie
Jun 15, 2015
Hey HoboFlo, can you tell me wich exactly Code you have put in to the "myImageUploadRequest()" Part or wich code have you delete in xcode??. Because i get en Error, wehn i just put your example "
var fileURL = NSURL(fileURLWithPath: audiofilePath)"
in Xcode. Thank you for your Time.


macrumors 68040
Sep 2, 2008
You both upload and email the file. Seems redundant. Why not use MFMailComposeViewController to email it directly?


macrumors 68000
Mar 27, 2009
You both upload and email the file. Seems redundant. Why not use MFMailComposeViewController to email it directly?
Maybe because MFMailComposerViewController opens up an email window, from which the user will have to hit "send". Although, barring any other reason for his app to store the email address than to send an email, he just might want to do that.