Skip to content

Instantly share code, notes, and snippets.

@dgfitch
Created August 29, 2019 15:06
Show Gist options
  • Save dgfitch/912346d3fae68dda26b59b03ae985bd7 to your computer and use it in GitHub Desktop.
Save dgfitch/912346d3fae68dda26b59b03ae985bd7 to your computer and use it in GitHub Desktop.
Simple web service endpoint for NIH Toolbox in PHP
<?php
$target_dir = "uploads/";
error_reporting(-1);
ini_set('display_errors', 'On');
date_default_timezone_set('America/Chicago');
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header('Content-Length: 0');
header('Connection: keep-alive');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Authorization, Origin, content-type, accept');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: OPTIONS, GET, POST');
return;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// PHP's $_FILES array is weird. This is handy to debug it.
//dump = var_export($_FILES, true);
//file_put_contents("output.log", $dump);
try {
$f = reset($_FILES);
$target_file = $target_dir . basename($f['name']);
move_uploaded_file($f["tmp_name"], $target_file);
header('Content-Type: application/json');
echo '{"error":"0"}';
http_response_code(200);
} catch (exception $e) {
header('Content-Type: application/json');
echo '{"error":' . $e->getMessage() . "\n" . $e->getTraceAsString() . '}';
http_response_code(500);
}
} else {
http_response_code(405);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment