PHP - Export csv delimited

Question:
I need help exporting this code from the csv file. The file with the data must be separated into columns, without errors with accents. I´ve already tried to input this structure fputcsv(PHP: fputcsv - Manual) but it dind´t work.

Repl link: https://replit.com/@vou-pedir/Order-To-CSV-New

114:
fputcsv($file, $headerRow, $delimiter = ",", $enclosure = '"', $escape_char = "");

130:
  // Write the item information to the file
    fputcsv($file, $itemArray);

135:
// Write the total row
$totalRow = ["Grand total", "", "", $total . $currency, ""];
fputcsv($file, $totalRow, $delimiter = ",", $enclosure = '"', $escape_char = "");

140:
$fileUrl = "https://" . $_SERVER['HTTP_HOST'] . "/" . $orderId . ".csv";

The $output hasn’t been defined in your code.

There is some things that you can change to make it better too.

$file = fopen($fileName, "w+");  // Added + for read/write mode.

You can change the file mode to "w+" from "w" . The w+ mode allows both reading and writing (you may not need now but it provides flexibility for later).

// Escreve o BOM (Byte Order Mark) para indicar UTF-8
fputs($file, chr(0xEF) . chr(0xBB) . chr(0xBF));

Change $output to $file. Ensure that special characters (like accents) are displayed correctly. (Proper UTF-8 Encoding).

// Build an array containing information about the item
$itemArray = [
    $item->ID,
    $item->Name,
    $item->Price . $currency,
    $item->Amount,
    $item->Price * $item->Amount . $currency,
    $item->Description
];
// Write the item information to the file
fputcsv($file, $itemArray);

Streamline the process of creating the item row for the CSV.

And construct the URL file using the host name so the link in the WhatsApp message will correctly point to the CSV file.

$fileUrl = "https://" . $_SERVER['HTTP_HOST'] . "/" . $fileName;

The full code modification will be something like this:

// Definir cabeçalhos para a codificação correta
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=exported_data.csv');
$fileName = $orderId . ".csv";
$file = fopen($fileName, "w+");  // Added + for read/write mode.

// Escreve o BOM (Byte Order Mark) para indicar UTF-8
fputs($file, chr(0xEF) . chr(0xBB) . chr(0xBF));

// Write the header row
$headerRow = ["ID", "Item", "Price", "Amount", "Total", "Description"];
fputcsv($file, $headerRow);

// Loop over each item in the $items array
foreach ($items as $item) {
    // Build an array containing information about the item
    $itemArray = [
        $item->ID,
        $item->Name,
        $item->Price . $currency,
        $item->Amount,
        $item->Price * $item->Amount . $currency,
        $item->Description
    ];
    // Write the item information to the file
    fputcsv($file, $itemArray);
}

// Write the total row
$totalRow = ["Grand total", "", "", $totalAmount, $total . $currency, ""];
fputcsv($file, $totalRow);

// Close the file
fclose($file);

$fileUrl = "https://" . $_SERVER['HTTP_HOST'] . "/" . $fileName;