getData(); // Create a callback to write the CSV data $callback = function () use ($data) { $file = fopen('php://output', 'w'); foreach ($data as $line) { // Convert the string into an array $fields = explode(',', $line); // Write the array to the CSV file fputcsv($file, $fields); } fclose($file); }; // Return a response with the CSV content return Response::stream($callback, 200, [ 'Content-Type' => 'text/csv', 'Content-Disposition' => 'attachment; filename="audition_entries_export.csv"', ]); } }