Note that there are some explanatory texts on larger screens.

plurals
  1. POFix file endcoding when downloading a file from Linux to Windows in php
    text
    copied!<p>Ok I have an issue. I have a Linux web server (RHEL 4 with apache 2) that is used to house an application. Part of this application is a set of php scripts. I created a script that accepts some form variables and then downloads a file to the user. Here si the code:</p> <pre><code>header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$destFileName); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($fullPath)); ob_clean(); flush(); readfile($fullPath); </code></pre> <p>This all works fine, and the file gets downloaded. But there is a problem. These files are being downloaded from the Linux box, to a Windows machine (ALWAYS). The problem is the encoding. When you look at the file on the Linux box all the text is aligned and all the columns look fine. (The files are just flat text files). But when the file gets downloaded onto the Windows box, and opened in Notepad, the file is all fouled up, and nothing is aligned. You also see weird charactors (the ones that look like a box, but that is just the generic representation for the unknow charactor). When this file is imported into another program, it does not work.</p> <p>However, when I open the file up in WordPad, all the text look correct. If I save it from wordpad it will import correctly, and look correct in Notpad.</p> <p>I don't have much knowlege on file encoding, so any information on how I can encode the file before sending to the user for download would be great.</p> <p>I did try replacing the <code>readfile($fullPath);</code> with:</p> <pre><code>$handle = @fopen($fullPath, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $buffer = str_replace('\n', '\r\n', $buffer); echo $buffer; } fclose($handle); } </code></pre> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload