3v4l.org

run code in 300+ PHP versions simultaneously
<?php // 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;
Output for 8.3.0 - 8.3.9
Warning: Undefined variable $orderId in /in/LUY5X on line 6 Warning: fopen(): open_basedir restriction in effect. File(.csv) is not within the allowed path(s): (/tmp:/in:/etc) in /in/LUY5X on line 7 Warning: fopen(.csv): Failed to open stream: Operation not permitted in /in/LUY5X on line 7 Fatal error: Uncaught TypeError: fputs(): Argument #1 ($stream) must be of type resource, false given in /in/LUY5X:10 Stack trace: #0 /in/LUY5X(10): fputs(false, '\xEF\xBB\xBF') #1 {main} thrown in /in/LUY5X on line 10
Process exited with code 255.
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.21
Warning: Undefined variable $orderId in /in/LUY5X on line 6 Warning: fopen(): open_basedir restriction in effect. File(.csv) is not within the allowed path(s): (/tmp:/in:/etc) in /in/LUY5X on line 7 Warning: fopen(.csv): Failed to open stream: Operation not permitted in /in/LUY5X on line 7 Fatal error: Uncaught TypeError: fputs(): Argument #1 ($stream) must be of type resource, bool given in /in/LUY5X:10 Stack trace: #0 /in/LUY5X(10): fputs(false, '\xEF\xBB\xBF') #1 {main} thrown in /in/LUY5X on line 10
Process exited with code 255.

preferences:
93.42 ms | 404 KiB | 67 Q