CSV Export

	$fh = fopen('php://output', 'w');
	        
	// Start output buffering (to capture stream contents)
	ob_start();

	  $headings = array('Action Tracker', 'Count');
	  
	  fputcsv($fh, $headings);

	  $item = array($action_tracker, $this_tracker_counted);
	  fputcsv($fh, $item);

	$string = ob_get_clean();
	        
	$filename = 'stats_report_' . date('Y_m_d') .'.csv';
        
    // Output CSV-specific headers
    $now = gmdate("D, d M Y H:i:s");
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    header("Last-Modified: {$now} GMT");

    // force download  
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    // disposition / encoding on response body
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");

exit($string);


Comments