This is an example PHP-script for receiving uploaded recordings via HTTP POST.


<?php

set_time_limit(0);

// CONFIGURATION
// where to store audio files
define('AUDIO_PATH', "/tmp");
// log file folder and filename (create folder before running script if you want logs)
define('AUDIO_LOG_FILE', "/tmp/audio-upload.log");
// upload file parameter name (don't change)
define('UPLOAD_NAME', "uploadedfile");

/**
* Fail the request and stop php processing
* @param string $mgs (optional) additional message that is sent with the response. default = ''
*/
function BadRequest($msg = '')
{
header('HTTP/1.1 400 Bad Request');
echo $msg;
exit;
}

/**
* Log event to upload log file
* @param string $message message to log
*/
function LogEvent($message)
{
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'no IP';
error_log("[" . date("Y-m-d H:i:s") . " {$ip}] {$message}" . PHP_EOL, 3, AUDIO_LOG_FILE);
}

// verify request
if (!isset($_FILES[UPLOAD_NAME]))
{
$msg = "No file uploaded!";
LogEvent($msg);
BadRequest($msg);
}

/// MAIN SCRIPT

// parse upload status
$error = $_FILES[UPLOAD_NAME]["error"];
$file_size = $_FILES[UPLOAD_NAME]["size"]; // in bytes
$file_name = basename($_FILES[UPLOAD_NAME]["name"]);
$file_temp = $_FILES[UPLOAD_NAME]["tmp_name"];

// Check upload status
if ($error != UPLOAD_ERR_OK)
{
LogEvent("{$file_name} ({$file_size} bytes): upload error({$error})!");
BadRequest("File upload failed");
}

// create save folder
$target_path = AUDIO_PATH;
if (!is_dir($target_path))
{
if (!mkdir($target_path, 0777))
{
LogEvent("{$file_name} ({$file_size} bytes): can't create save folder ({$target_path})!");
BadRequest("folder creation failed");
}
}

// move temporary file to right folder
$full_name = $target_path . "/" . $file_name;
if (!move_uploaded_file($file_temp, $full_name))
{
LogEvent("{$file_name} ({$file_size} bytes): temp file move failed ({$file_temp} --> {$target_path})");
BadRequest("move failed");
}

// Transfer OK
LogEvent("{$full_name} ({$file_size} bytes): OK");

// This will confirm the client that transfer was OK and file can be deleted locally
echo $file_name;