file($file['tmp_name']), FILE_ACCEPTED_MIME_TYPES, true)) { throw new RuntimeException("Invalid file format."); } $file_id = generate_random_char_sequence(FILE_ID_CHARACTERS, FILE_ID_LENGTH); if (!move_uploaded_file($file['tmp_name'], FILE_DIRECTORY . sprintf('/%s.%s', $file_id, $file_ext))) { throw new RuntimeException("Failed to save the file. Try again later."); } json_response([ 'id' => $file_id, 'ext' => $file_ext, 'mime' => FILE_ACCEPTED_MIME_TYPES[$file_ext] ], null, 201); } catch (RuntimeException $e) { json_response(null, $e->getMessage(), 400); }