The browser sees application/octet-stream as a binary type. You need a text/plain content type:
header('Content-Type: text/plain'); // Or: header('Content-Type: text/csv'); // Or: header('Content-Type: application/csv');
The Content-Transfer-Encoding header should be unnecessary if you installed the Content-Type correctly, and indeed, it is likely to mislead the browser, thinking that it received the binary:
// No need, possibly harmful. Remove it... // header('Content-Transfer-Encoding: binary');
Update:
I see another problem. You set the Content-Length not the file size, but rather the file descriptor opened with fopen() , which incorrectly informs the browser about the number of bytes expected. filesize() takes the file name string as its argument, not a file descriptor. You probably need to close the handle with fclose($NewFile) before calling filesize() .
// Instead of: header('Content-Length: ' . filesize($NewFile)); // You mean to use $FileName // close the file handle first... fclose($NewFile); header('Content-Length: ' . filesize($FileName));
source share