arturo322 Posted March 8, 2011 Share Posted March 8, 2011 Hi i turned my output_buffering to true on my php.ini because i need some of its features for trailing the system my problem now is when im getting .csv file using header function, it seems that it gives me the source code of the interpreted code instead of the expected .csv contents. Here is the source code echo $who; echo $ecrn; $myFile = "($who)$info_value[RecordName]-$ecrn"; header('Content-Type: text/csv; charset=utf-8'); $content = "Content-Disposition: attachment; filename=".$myFile.".csv"; header($content); $output = fopen('php://output', 'w'); $sql = "SELECT * FROM classrecordvalue"; $query = mysql_query($sql); // loop over the rows, outputting them while ($row = mysql_fetch_array($rows)) fputcsv($output, $row); im just wondering does output_buffering true in php.ini affect the output here? if so any other suggestions there of how i can download .csv files using php? thank you much for the people who will help Link to comment https://forums.phpfreaks.com/topic/229982-unexpected-output-when-downloading-csv-using-header-function/ Share on other sites More sharing options...
kenrbnsn Posted March 8, 2011 Share Posted March 8, 2011 What are you trying to do? AFAIK, you don't read files using the header() function. If you want to read a CSV file, you should use fgetcsv(). Ken Link to comment https://forums.phpfreaks.com/topic/229982-unexpected-output-when-downloading-csv-using-header-function/#findComment-1184493 Share on other sites More sharing options...
arturo322 Posted March 8, 2011 Author Share Posted March 8, 2011 oh, sir what i mean is i want to download the .csv file, but then the problem is the downloaded file that im getting contains html tags of the whole page Link to comment https://forums.phpfreaks.com/topic/229982-unexpected-output-when-downloading-csv-using-header-function/#findComment-1184499 Share on other sites More sharing options...
kenrbnsn Posted March 8, 2011 Share Posted March 8, 2011 Change your code to: <?php echo $who; echo $ecrn; $myFile = "($who){$info_value['RecordName']}-$ecrn"; header('Content-Type: text/csv; charset=utf-8'); $content = "Content-Disposition: attachment; filename=".$myFile.".csv"; header($content); $sql = "SELECT * FROM classrecordvalue"; $query = mysql_query($sql); // loop over the rows, outputting them while ($row = mysql_fetch_array($rows, MYSQL_NUM)){ echo '"' . implode('","',$row) . '"' . "\n"; } ?> or you can write a temp file and then use readfile(). <?php echo $who; echo $ecrn; $myFile = "($who){$info_value['RecordName']}-$ecrn"; header('Content-Type: text/csv; charset=utf-8'); $content = "Content-Disposition: attachment; filename=".$myFile.".csv"; header($content); $sql = "SELECT * FROM classrecordvalue"; $query = mysql_query($sql); // loop over the rows, outputting them $fn = tempnam('/tmp','csv'); $tmp = array() while ($row = mysql_fetch_array($rows, MYSQL_NUM)){ tmp[] = '"' . implode('","',$row) . '"'; } file_put_contents($fn,implode("\n",$tmp)); $fs = filesize($fn); header("Content-Length: $fs"); readfile($fn); unlink($fn); //delete the temporary file ?> Ken Link to comment https://forums.phpfreaks.com/topic/229982-unexpected-output-when-downloading-csv-using-header-function/#findComment-1184511 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.