php - Problems with putcsv and force-download -
i trying create csv file upon button click. problem have file wont download, echos out content.
this code
class csv_writer { public $dataarray; public $filename; public $csvheader; public function create_csv() { /** * * creates csv , make ready download */ $fp = fopen('php://output', 'w'); fprintf($fp, chr(0xef).chr(0xbb).chr(0xbf) ); fputcsv($fp, $this->csvheader, ';',' '); foreach ( $this->dataarray $ferow ) { fputcsv($fp, $ferow,';'); } header('content-encoding: utf-8'); header('content-type: text/csv; charset=utf-8'); header('content-disposition: attachment; filename="'.$this->filename.'.csv"'); fclose($fp);} }
this how call class:
$csv = new csv_writer(); $csv->dataarray = array( 'value1', 'value2' ); $csv->filename = 'test'; $csv->csvheader = array( 'value1', 'value2' ); $csv->create_csv();
i have tried every other exmaple here on stack, no luck! there bright mind out there can me out!
you haven't produced content, filename.
here follows codesnippet wrote time ago.
it create output.
in short: first set headers (including filenamesuggestion).
then content.
$filenamesuggestion = "database_export_".date("d")."-".date("f")."-".date("y").".sql"; header("content-disposition: attachment; filename=\"".$filenamesuggestion."\""); header("cache-control: no-cache, must-revalidate"); // http/1.1 header("expires: sat, 26 jul 1997 05:00:00 gmt"); // date in past // create content here, need echo or create output way // ..... exit;
i hope helps going.
Comments
Post a Comment