<?php

// saveDataCSV.php  01/04/20
// purpose: read data file from server and output it to browser for download as CSV

// get experiment data from file
$labdata = file("LexicalData.txt");
$numRows = count($labdata);
//echo("ImpressData.txt has $numRows rows");

$outputFileName = "LexicalData.csv";

ob_clean();
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $outputFileName);    
if(isset($labdata['0'])){
	$fp = fopen('php://output', 'w');
	for ($i=0; $i<$numRows; $i++) {	
		$rowdata = explode(",",$labdata[$i]);
		if (count($rowdata) > 1) { 
			for ($j=0; $j<count($rowdata); $j++) {
	    		$rowdata[$j] = trim($rowdata[$j]);
			}
			fputcsv($fp, $rowdata);
		}
	}
	fclose($fp);
}
ob_flush();
 
?>
