title: PHP using iconv to convert large files
body: <p>I need to convert text files' character encodings without hogging the server's memory, while the input file is user configured and its size isn't limited. </p>
  
  <p>Would it be more efficient to wrap an unix's iconv command using exec() (which I'd rather avoid, although I already use it in the application for other file operations), or should I read the file line by line and output it into another file?</p>
  
  <p>I'm thinking working this way:</p>
  
  <pre><code>$in = fopen("in.txt", "r");
  $out = fopen("out.txt", "w+");
  while(($line = fgets($in, 4096)) !== false) {
      $converted = iconv($charset["in"], $charset["out"], $line);
      fwrite($out, $converted);
  }
  rename("out.txt", "in.txt");
  </code></pre>
  
  <p>Is there any better approach to convert the file fast and efficiently? I'm thinking this might be rather CPU intensive, but then I believe iconv itself is an expensive task so I'm not sure if I can make it actually not eat the server much at all.</p>
  
  <p>Thanks!</p>