PHP export data to CSV

I can get the file to write to the server perfectly. What I am wanting to do is to have it download automatically through the browser - which it does - but the file is unreadable. I think I have been stuck on this so long I’ve forgotten knowledge and started throwing darts. Here is what I have now:

[php]$NewFile = fopen($FileName,“w”);
fwrite($NewFile, $output);
header(‘Content-Type: text/csv; charset=utf-8’);
header(‘Content-Disposition: attachment; filename="’.$FileName.’"’);
fclose($NewFile);
header('Content-Length: '.filesize($FileName));[/php]

The above is from asking some other people and here is what I started with. Both yield the same results:

[php]$NewFile = fopen($FileName,“w”);
fwrite($NewFile, $output);
fclose($NewFile);
//print_r($output);
header(‘Content-type: text/csv; charset=UTF-8’);
header(“Content-type: application/force-download”);
header(‘Content-Description: File Transfer’);
header(‘Content-Disposition: inline; filename="’.$NewFile.’"’);
header(“Content-length: “.filesize($NewFile));
//header(‘Content-Type: text/csv’);
header('Content-Disposition: attachment; filename=”’.$FileName.’”’);
readfile($NewFile);
@unlink($FileName);[/php]

Both write a file to the server that opens fine in OpenOffice as a CSV from the server or FTP but the file which downloads through the browser is encoded - not sure how. It looks like this: ��ࡱ�

Hi,

How do you download that csv file? I’ve tried to download by typing the full url address:
http://www.example.com/file_name.csv

and i manage to download that without any problem. I tried to open the file and its not encoded.

Regards,
developer.dex2908

Try adding

[php]header(“Content-Transfer-Encoding: binary”); [/php]

Noodles I tried that. In fact I’ve tried just about every type of encoding header there is. I can’t think of anything server side which would impact this but maybe there is?

maybe is happening what it is happening to me on one of my script. that the browser input HTML above the file
so right click the downloaded file and EDIT with notepad++ to see if there is any html outputs from the browser

Sponsor our Newsletter | Privacy Policy | Terms of Service