How to download a file directly instead of reading it from the html

i have this code:

        $garages = $this->formatGaragesData($garageEntities);
        // Returns an array with multiple arrays in it which are containing values like name etc

        $fileName = 'garage_export.csv';

        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
        header("Content-Transfer-Encoding: UTF-8");

        foreach ($garages as $garage) {
            foreach ($garage as $value) {
                echo $value;
            }
        }

this downloads my garage_export.csv file but it does not place the values in the correct columns, so i thought is there a way to download garage_export.csv directly instead of reading it from the html because in garage_export.csv the data is placed in the correct columns.

Any help or advice is welcome 😀