Array to CSV file download in Laravel 5.1

Rajat picture Rajat · Apr 28, 2017 · Viewed 7.3k times · Source

I am having an array and i am trying to download it as csv file my array looks like this

Array(
[0] => Array
    (
        [Date] => 2017-01-02
        [Name] => Asia consumer MC Alpha bmk - risk
        [Level] => .333000
    )

[1] => Array
    (
        [Date] => 2017-01-03
        [Name] => Asia consumer MC Alpha bmk - risk
        [Level] => .333000
    ))

my code to convert the array to file is

public function DownloadFile($filetype,$Array_data,$content){

    $LevelArray = array('Date','Name','Level');

    $selected_array =  $LevelArray;
    if($filetype =='csv'){

            $Filename ='Level.csv';
            header('Content-Type: text/csv; charset=utf-8');                
            header('Content-Disposition: attachment; 
            filename='.$Filename.'');
            // create a file pointer connected to the output stream
            $output = fopen('php://output', 'w');
            fputcsv($output, $selected_array);
            foreach ($Array_data as $row){
                fputcsv($output, $row);
            }
            fclose($output);                
        }}

in response i am getting the output on response but that response is not converted into a file type though headers are pointing to get the csv file from it.

 Date,Name,Level
2017-01-02,"Asia consumer MC Alpha bmk - risk",.333000
2017-01-03,"Asia consumer MC Alpha bmk - risk",.333000
2017-01-04,"Asia consumer MC Alpha bmk - risk",.333000

I have tried using response method of laravel and many others too nothings seems to work, its headers not able to force browser to download the file

RESPONSE Headers look like this

Connection:close
Content-Disposition:attachment; filename=Benchmark-Level.csv
Content-type:text/csv;charset=UTF-8
Host:localhost:8000
X-Powered-By:PHP/5.6.25

Answer

waterloomatt picture waterloomatt · Apr 28, 2017

Is your 2nd header split over 2 lines? It needs to be a single line with no line breaks.

header('Content-Disposition: attachment; filename='.$Filename.'');

Edit

This code is working for me. I added one more header to force download, but it works in Chrome with or without that extra header. Could you please try running it in different browsers? My thought is that there are browser settings preventing the download.

<?php

$selected_array = array('header:col1','header:col2', 'header:col3');

$Array_data = array(
    array('row1:col1','row1:col2', 'row1:col3'),
    array('row2:col1','row2:col2', 'row2:col3'),
    array('row3:col1','row3:col2', 'row3:col3'),
);

$Filename ='Level.csv';
header('Content-Type: text/csv; charset=utf-8');
Header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$Filename.'');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
fputcsv($output, $selected_array);
foreach ($Array_data as $row){
    fputcsv($output, $row);
}
fclose($output);
?>