title: PHP: How to put out text/plain content in UTF-8 with BOM for download?
body: <p>I need to offer a plain text file for download. The text file needs to be UTF-8 encoded and needs a BOM to be present. I saved my php file as UTF-8 without BOM and send the following headers:</p>
  
  <pre><code>header('HTTP/1.1 200 OK');
  header('Content-Type: text/plain; charset=utf-8');
  header('Content-Disposition: attachment; filename="test.txt"');
  </code></pre>
  
  <p>I save the script without BOM because it would interfere with sending the headers.
  So I tried putting a BOM manually by:</p>
  
  <pre><code>echo chr(239).chr(187).chr(191);
  </code></pre>
  
  <p>Then I put out my text. Without The manual BOM an editor like Notepad++ will recognize the file to be ANSI encoded, with the supposed manual BOM it will be recognized as UTF-8 but will contain the characters:</p>
  
  <pre><code>ï»¿
  </code></pre>
  
  <p>at the start. So I assume it is detected to be UTF-8 by means of heuristics and my manual BOM is wrong.</p>
  
  <p>How do I do it right?</p>
  
  <p><strong>EDIT:</strong> HEX contents as requested. I simply made the text "SOME TEXT" and I get:</p>
  
  <pre><code>C3 AF C2 BB C2 BF 53 4F 4D 45 20 54 45 58 54
  </code></pre>
  
  <p>Saving "SOME TEXT" as UTF-8 with BOM yields:</p>
  
  <pre><code>EF BB BF 53 4F 4D 45 20 54 45 58 54
  </code></pre>