title: Detect file encoding in PHP
body: <p>I have a script which combines a number of files into one, and it breaks when one of the files has UTF8 encoding. I figure that I should be using the <code>utf8_decode()</code> function when reading the files, but I don't know how to tell which need decoding.</p>
  
  <p>My code is basically:</p>
  
  <pre><code>$output = '';
  foreach ($files as $filename) {
      $output .= file_get_contents($filename) . "\n";
  }
  file_put_contents('combined.txt', $output);
  </code></pre>
  
  <p>Currently, at the start of a UTF8 file, it adds these characters in the output: <code>ï»¿</code></p>