PHP-Script failing to write filenames into csv

I have a PHP script running on a web server for a university project. I have a corresponding app sending pictures and additional data to the server. The script is supposed to take the data, write it into a CSV file, and save the pictures. The problem is that I can’t get my script to write the image names into the CSV files. The pictures are correctly named, but I can’t get the names into the CSV file.

The csv still has the “test1”, “test2”, “test3” values.

What am I missing?

<?php
// Set CORS headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $postData = file_get_contents('php://input');
    $decodedData = json_decode($postData, true);

    // Initialize variables for image paths and filenames
    $imageFilePaths = [];
    $imageFileNames = ["test1","test2","test3"]; // Array for image filenames

    // Check file uploads, process up to three images
    $count = 0;
    $testvariable = null; // Initialize variable outside foreach loop
    foreach ($_FILES as $key => $value) {
        if ($count >= 3) break; // Process only up to three images
        if ($value['error'] === UPLOAD_ERR_OK) {
            $imageTmpName = $value['tmp_name'];
            $imageFileName = $value['name']; // Original filename of the image
            $testvariable = $imageFileName; // Update variable with the filename of the first image
            $imageFilePath = '/var/www/html/geodata/images/' . $imageFileName; 

            // Save image
            if (!move_uploaded_file($imageTmpName, $imageFilePath)) {
                echo json_encode(['status' => 'error', 'message' => 'Error saving the image']);
                exit;
            }
            $imageFilePaths[] = $imageFilePath; // Add file path to the array
            $imageFileNames[] = $value['name']; // Add filename to the array
            $count++;
        }
    }

    if ($decodedData) {
        // Extract data from decoded JSON
        $reportId = $decodedData['report']['reportId'] ?? '';
        $userId = $decodedData['report']['userId'] ?? '';
        $timestamp = $decodedData['report']['timestamp'] ?? '';
        $latitude = $decodedData['report']['latitude'] ?? '';
        $longitude = $decodedData['report']['longitude'] ?? '';
        $deficiencyType = $decodedData['report']['deficiencyType'] ?? '';
        $onPublicProperty = $decodedData['report']['onPublicProperty'] ?? '';
        $additionalInformation = $decodedData['report']['additionalInformation'] ?? '';

        // Write data to the CSV file
        $csvFilePath = '/var/www/html/geodata/data.CSV';
        $csvFile = fopen($csvFilePath, 'a');

        if ($csvFile) {
            // Check if images were uploaded
            if (!empty($imageFileNames)) {
                // Add filenames of all uploaded images to the dataset
                $data = [$reportId,$testvariable, $userId, $timestamp, $latitude, $longitude, $deficiencyType, $onPublicProperty, $additionalInformation];
                $data = array_merge($data, $imageFileNames); // Use filenames instead of file paths
                fputcsv($csvFile, $data);
            } else {
                fputcsv($csvFile, [$reportId, $userId, $timestamp, $latitude, $longitude, $deficiencyType, $onPublicProperty, $additionalInformation]);
            }
            fclose($csvFile);
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Error writing to the CSV file']);
            exit;
        }

        // Add data to the HTML content
        $htmlFilePath = '/var/www/html/index.html';
        $htmlContent = file_get_contents($htmlFilePath);

        $appendedContent = "<p>Report ID: " . $reportId . ", User ID: " . $userId . ", Timestamp: " . $timestamp . "</p>n";
        $updatedHtmlContent = $htmlContent . $appendedContent;

        // Save updated content back to index.html
        file_put_contents($htmlFilePath, $updatedHtmlContent);

        echo json_encode(['status' => 'success', 'message' => 'Data successfully written']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Invalid JSON format']);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'No POST request']);
}
?>