array( 'method' => 'GET', 'header' => "Accept: application/json\r\n" ) )); $body = file_get_contents($url, false, $context); if ($body != false) { header("Content-Disposition: attachment; filename=\"$filename\""); header('Content-type: application/json'); echo $body; } else { header('HTTP/1.1 404 Not Found'); } } else if (isset($_GET['id'])) { // retrieve the file with given id from disk, return it, // and remove it from disk $id = $_GET['id']; $body = file_get_contents(getFilename($id)); if ($body !== false) { header("Content-Disposition: attachment; filename=\"$filename\""); header('Content-type: application/json'); echo $body; unlink(getFilename($id)); } else { header('HTTP/1.1 404 Not Found'); } } else { // TODO: error } } else if ($method == 'POST') { // retrieve the data, save it on disk with a random id, // and return the id. if (isset($_FILES['file'])) { // read body from uploaded form $file = $_FILES['file']; $id = uniqid(); $filename = getFilename($id); move_uploaded_file($file['tmp_name'], $filename); echo $id; } else { // read raw body from post request $body = @file_get_contents('php://input'); if ($body === false) { $body = ''; } $id = uniqid(); file_put_contents(getFilename($id), $body); echo $id; } } // cleanup files older than 1 hour // http://stackoverflow.com/q/6411451/1262753 if ($dir = opendir($tmp)) { $now = time(); while (false !== ($file = readdir($dir))) { $filename = "$tmp/$file"; if (is_file($filename) && filemtime($filename) <= ($now - 60 * 60) ) { unlink($filename); } } closedir($dir); } ?>