HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/SendToKipu/index25052025.php
<?php
// Enable error reporting for development
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set your API key
$validApiKey = "kPpUNPQgaJngyUNr7iRIXbcalSfm6PQAw0fNi";

// Get headers
$headers = getallheaders();

// Check API key
if (!isset($headers['api-key']) || $headers['api-key'] !== $validApiKey) {
    http_response_code(401);
    echo json_encode([
        "status" => "error",
        "message" => "Unauthorized: Invalid or missing API key"
    ]);
    exit;
}

// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode([
        "status" => "error",
        "message" => "Method Not Allowed. Use POST."
    ]);
    exit;
}

// Get JSON or form-data
$data = [];
if (isset($_SERVER["CONTENT_TYPE"]) && strpos($_SERVER["CONTENT_TYPE"], "application/json") !== false) {
    // JSON body
    $input = file_get_contents("php://input");
    $data = json_decode($input, true);
} else {
    // Form-data or x-www-form-urlencoded
    $data = $_POST;
}

// Required text fields
// $requiredFields = [
//     "first_name", "last_name", "email", "phone", "ssn", "dob", "gender",
//     "insurance_company", "policy_no", "group_id", "insurance_phone",
//     "subscriber_dob", "insurance_attributes", "facility_admit_to",
//     "address_state", "address_city", "address_country", "address_street",
//     "address_zip_code"
// ];
$requiredFields = [
    "first_name", "last_name", "email"
];

// Required file fields
$requiredFiles = ["file1", "file2", "file3", "file4", "file5"];

// Validate required text fields
$missing = [];
foreach ($requiredFields as $field) {
    if (!isset($data[$field]) || trim($data[$field]) === "") {
        $missing[] = $field;
    }
}

// Validate required file fields
foreach ($requiredFiles as $fileKey) {
    if (!isset($_FILES[$fileKey]) || $_FILES[$fileKey]['error'] !== UPLOAD_ERR_OK) {
        // $missing[] = $fileKey;
    }
}

if (!empty($missing)) {
    http_response_code(400);
    echo json_encode([
        "status" => "error",
        "message" => "Missing required fields or files",
        "missing" => $missing
    ]);
    exit;
}

// ✅ Save uploaded files
// ✅ Save uploaded files
$uploadedFiles = [];
$uploadDir = __DIR__ . "/uploads/";
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

foreach ($requiredFiles as $fileKey) {
    $filename = basename($_FILES[$fileKey]['name']);
    $targetPath = $uploadDir . $filename;
    $uploadedFiles[$fileKey] = $filename;
    // if (move_uploaded_file($_FILES[$fileKey]['tmp_name'], $targetPath)) {
    //     // Store only the filename
    //     $uploadedFiles[$fileKey] = $filename;
    // } else {
    //     $uploadedFiles[$fileKey] = null;
    // }
}

// ✅ Merge files into data_received
$dataWithFiles = array_merge($data, $uploadedFiles);

// ✅ Example: Return data
$response = [
    "success" => true,
    "response-code" => "200",
    "message" => "Data send to kipu successfully",
    "data_received" => $dataWithFiles
];

header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);