1

Is that a way to custom the header of a generated csv file :

$result = $db->select(" table","",""," fitstname, lastname, campany");
$headers = array("fistname","lastname","campany"); 
$fp = fopen('php://output', 'w'); 

if ($fp && $result) {     
    header('Content-Type: text/csv ; charset=utf-8');
    header('Content-Disposition: attachment; filename="recap.csv"');
    header('Pragma: no-cache');    
    header('Expires: 0');

    fputcsv($fp, $headers); 
    for ($i= 0; $i < sizeof($result); $i++) {
        fputcsv($fp, array_values($result[$i])); 
    }
    die; 
} 

updated : here the output :

nom,prenom,societe
Charles,Olivier,"Zed way"
Bernard,Gautier,"Media Productions"

Regards .

Kyle Hale
  • 7,912
  • 1
  • 37
  • 58
user3911183
  • 787
  • 1
  • 12
  • 34

2 Answers2

1

On php.net you can find helpfull user comments to nearly every
(un)documented function and feature.
This answers your question very well.. >> php.net/manual/en/function.fputcsv.php#104980


Update:

Well as you can read in the first line, when hit the link above, it's sufficient to do this:

<?php $out = fopen('php://output', 'w');fputcsv($out, array('this','is some', 'csv "stuff", you know.'));fclose($out); ?>

This will directly output your csv in the browser window...
If you wnat to make the browser handling it as a download, then you must do the header-things... What is it you want to do?

useDuser
  • 21
  • 5
0

The short answer: CSV files contain only data. There's no formatting information, and no structural information beyond field and record delimiters.

The longer answer: You can include formatting and other information if you target the output file to a specific application. Of course, then it ceases to be a CSV file. If you want to output to Excel format you can use a library such as PHPExcel. There are other libraries that would allow output to PDF, Word, or other application formats, or you could just generate an HTML table.