-1

I have a function which currently creates a file and stores it in a folder on my server.

I want to continue doing this but I also want it to force to download.

This is the function:

// Generates a filename and writes a CSV with a timestamped name
function putAsCSV($data_list,$account_name,$timeframe){
    $date =  date('d-m-Y-h-i-A');
    $filename = $timeframe.'-'.$account_name.'-'.$date;
    $fp = fopen('../exports/'.$filename.'.csv', 'w');
    foreach ($data_list as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);
}

How can I make it download this file as well as storing it?

Do I need to store it in some sort of temporary file?

How can I ensure it doesn't open in the browser and actually downloads?

Francesca
  • 26,842
  • 28
  • 90
  • 153
  • Take a look at http://php.net/manual/en/function.header.php, you just need to set the correct headers (Content-Type and Content-Disposition) and you can force a download – naththedeveloper Jan 27 '15 at 12:51
  • This will help too http://stackoverflow.com/a/1465591/1301076 – rjdown Jan 27 '15 at 12:51

1 Answers1

1

You should utilize the native PHP function readfile for sending data down the wire. It'll prevent clogging up the pipe.

http://php.net/manual/en/function.readfile.php

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Richard Denton
  • 982
  • 2
  • 7
  • 13