FatFree prepare CSV for download, from database

500 views Asked by At

What would be the right way of getting CSV as a file download, prepared from a database table. I would like this to happen on a button click.

Should I first make that .csv using standard way of doing it in PHP, then use \Web::instance()->send() in some route handler?

1

There are 1 answers

0
xfra35 On

Web->send expects a real file as input, so you'll have to go the standard PHP way:

function dumpCSV($rows,$filename,$sep=',',$eol="\r\n") {
  header('Content-type: application/csv; charset=UTF-8');
  header('Content-Disposition: attachment; filename='.$filename);
  foreach($rows as $row)
    echo implode($sep,array_map(function($cell){
      return '"'.$cell.'"';
    },$row)).$eol;
}

Also checkout ikkez's Sheet class, which may help you rendering tabular data in Excel and CSV format.